Forum Moderators: open

Message Too Old, No Replies

form fields calculator

         

kaidoparv

1:31 pm on Dec 30, 2010 (gmt 0)

10+ Year Member



is there any good example to make a javascript calculator which can calculate te total sum from quantity fields and checkboxes? for example i have to lines of data, each can have different quantity and 2 checkbox. increasing the quantity will increase the sum 10 EUR (for one line, second can have different sum), checking some checkboxes will increases the total sum too, but also all the checkboxes may have different sums, like 5 EUR, 0,5 EUR, 2,49 EUR etc

some examples?

MichaelBluejay

2:14 pm on Jan 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not really following you, but if you want a checkbox to increase the sum in a total field, then this will work:

<script type="text/javascript"> 
function add(howMuchToAdd) {
totalField = document.getElementById("total");
totalField.value = totalField.value-0+howMuchToAdd;
}
</script>

<input type="checkbox" onclick="if (this.checked) add(10); else add(-10)">
<input type="text" id="total">


The reason we subtract 0 before adding the new value is that tells JavaScript we're working with a number. Otherwise it might think the value is a string, so adding 5 + 6 could result in "56".