-
June 18th, 2012, 12:32 PM
#1
Help , just beginning javascript....
So what I am trying to do is pass the value of 2 text boxes into a javascript function add them together (or subtract) and then populate the answer in another text box... the code is:
Code:
<script language="javascript" type="text/javascript">
function setamount(amt1,amt2) {
var amount1=parseInt(amt1);
var amount2=parseInt(amt2);
var total=amount1+amout2;
document.form1.total.value=total;
}
</script>
HTML Code:
<td><input type="text" id="amnt1" maxlength=3 /><td></td>
<td><input type="text" id="amnt2" maxlength=3 onBlur="setamount(document.getElementById('amnt1').value,document.getElementById('amnt2').value)"/><td></td>
<td><input type="text" id="total" maxlength=3 /><td></td>
Im using the onBlur event just because i dont havea button on my form.
The javascript stops working when i try to calculate the total!
Thanks in advance
-
June 18th, 2012, 12:40 PM
#2
Re: Help , just beginning javascript....
sorry there are a couple of extra <td> in the HTML. They arent in the original version
-
June 18th, 2012, 10:29 PM
#3
Re: Help , just beginning javascript....
You have a spelling error in your declaration of total. Also...I'd suggest some code cleanup.
Code:
<script type="text/javascript">
function setamount() {
var amount1 = parseInt(document.getElementById("amnt1").value, 10);
var amount2 = parseInt(document.getElementById("amnt2").value, 10);
document.getElementById("total").value = amount1 + amount2;
}
</script>
<td><input type="text" id="amnt1" maxlength=3 /></td>
<td><input type="text" id="amnt2" maxlength=3 onBlur="setamount()"/></td>
<td><input type="text" id="total" maxlength=3 /></td>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|