Samir wrote:
Here is a sample form that I just made up. I would like a somekind of
script that when each button is checked or unchecked to dynamicly add or
subtract the total. If anyone can help, thanks alot.
[snip]
Add an "onchange" function to each checkbox that calls a function to
cycle through the checkboxes and add up those that are checked, e.g.:
<p><input type="checkbox" name="C1" value="50"
onchange="writeTotal(this.form,'total');">item 1 $50</p>
then write the total to the paragraph.
function writeTotal(x,y) {
var total = 0;
for (var i=0;i<x.elements.length; i++) {
if (x.elements[i].type == 'checkbox'
&& x.elements[i].checked) {
total += +x.elements[i].value;
} }
document.getElementById(y).innerHTML = total;
}
Of course this is just a minimal example, you need to add feature
detection and handle cases where the user has JavaScript disabled.
You'll need a custom reset button because the normal one will not
change the total in the paragraph:
<input type="button" value="Clear all"
onclick="this.form.reset();writeTotal(this.form,'t ot');">
Also, you need to ensure that when the page is re-loaded, the form is
re-set as some browsers will clear the check boxes but not update the
paragraph text (or vice versa) - say an 'onload' function to reset the
form.
You can add an id to the para, or put in a <span> with an id and just
write the number:
<p>Total $ <span id="total">0</span></p>
I'd put your form into a table, then use character alignment to get all
your numbers to line up.
Cheers, Fred.