CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    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

  2. #2
    Join Date
    Jun 2012
    Posts
    2

    Re: Help , just beginning javascript....

    sorry there are a couple of extra <td> in the HTML. They arent in the original version

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    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
  •  





Click Here to Expand Forum to Full Width

Featured