Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding text fields

Status
Not open for further replies.

mortkc

MIS
Apr 23, 2001
84
US
I have a form with 7 text fields used for entering dollar values. Is there a way that I can call on javascript to keep a running total in the subtotal field as I enter numbers in the other text boxes? I am very new to Javascript, and am not certain how to to this. Any help would be grately appreciated. Thanks.
 
Here's a small working example, I'd use onblur or something similar:
Code:
<html>
<head>
<title>Blah</title>
<script language=JavaScript>
function updateSum() {
   sum = 0;
   for(i = 0;i < 4;i++) {
      num = document.getElementById("text" + i).value;
      num = (num.length == 0) ? 0 : num;
      sum += parseInt(num, 10);
   }
   blahForm.sum.value = sum;
}
</script>
</head>
<body>
<form name=blahForm>
<input type=text id=text0 onblur='updateSum()'>Value 1<br>
<input type=text id=text1 onblur='updateSum()'>Value 2<br>
<input type=text id=text2 onblur='updateSum()'>Value 3<br>
<input type=text id=text3 onblur='updateSum()'>Value 4<br><br>
<input type=text name=sum>Sum
</form>
</body>
</html>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top