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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Getting a Form to add figures to make totals

Status
Not open for further replies.

ltleforge

Technical User
Oct 11, 2003
7
0
0
GB
I am trying to find a solution in creating a form that contains check boxes and radio buttons. Next to the buttons are amounts for example $4.00 when I click on these amounts I need for them to update in the total box at the bottom. And obviously when I click off the radio or checkbox the amount should deduct.

Can anyone please please please help me with a simple solution.
 
Just write a function that either loops through or looks at each element, examines the .checked property and adds the value to the total.

Then call this function on the onclick or onchange events of each element.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Code:
function addalltotals() {
   sum = 0;
   if (formname.whatever.checked == true) {
      sum = sum + field containing amount;
   }
   .... continue for all items, loop them if your naming convention permits
   formnam.totalfield.value = sum;
}

Then run that as an onchange event handler for each checkbox and radio button, that way you don't have to worry about decrementing any amounts, just calculate the value from scratch each time.

-kaht
 
dwarfthrower beat me to the submit button
 
Gotta be quicker than that kaht ;-)

Nice pseudo-code btw.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
I wouldn't want to loop thru every form element everytime the user click (or un-clicked) an entry... how bout...
Code:
<script type=&quot;text/javascript&quot;>
function upd(what){
  document.theForm.sum.value += (what.checked) ? what.value:(0-what.value);
}
</script>
<form id=&quot;theForm&quot;>
<input id=&quot;cb1&quot; type=&quot;checkbox&quot; value=&quot;1&quot; onchange=&quot;upd(this);&quot;>
<input id=&quot;cb2&quot; type=&quot;checkbox&quot; value=&quot;1.75&quot; onchange=&quot;upd(this);&quot;>
<input id=&quot;cb3&quot; type=&quot;checkbox&quot; value=&quot;2.5&quot; onchange=&quot;upd(this);&quot;>
<input id=&quot;sum&quot; type=&quot;text&quot; value=&quot;0.00&quot; readonly>
</form>
 
Aarrgh. Just tested that and it splats like a pig on roller skates. Try:

Code:
<html>
<body>
<script type=&quot;text/javascript&quot;>
function upd(what){
  var newSum = new Number();
  newSum = document.getElementById('sum').value
  newSum += (what.checked) ? '+' + what.value:'-' + what.value;
  document.getElementById('sum').value = eval(newSum);
}
</script>
<form id=&quot;theForm&quot;>
<input id=&quot;cb1&quot; type=&quot;checkbox&quot; value=&quot;1&quot; onchange=&quot;upd(this);&quot; onclick=&quot;blur();&quot;> 1.0<br>
<input id=&quot;cb2&quot; type=&quot;checkbox&quot; value=&quot;1.75&quot; onchange=&quot;upd(this);&quot; onclick=&quot;blur();&quot;> 1.75<br>
<input id=&quot;cb3&quot; type=&quot;checkbox&quot; value=&quot;2.5&quot; onchange=&quot;upd(this);&quot; onclick=&quot;blur();&quot;> 2.5<br>
<input id=&quot;sum&quot; type=&quot;text&quot; value=&quot;0.00&quot; readonly>
</form>
</body>
</htm>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top