Running total on checkboxes

5 views
Skip to first unread message

Jeffrey Rees

unread,
Jan 5, 2017, 3:46:53 PM1/5/17
to HEO iForms
hello all - 

Basically I want to keep a running total of different values depending on which check boxes are selected. I found a function that works but only if all of my checkboxes have the same name. 

Is there a way to send these to VGR with the same checkbox name, or is there a way to have these check boxes have unique names but still keep my function working properly?
I hope all is well!


Here is an example:

function totalIt() {
var el, i = 0;
var total = 0;
while(el = document.getElementsByName("product")[i++]) {
if(el.checked) { total= total + Number(el.value);}
}
document.getElementById("total").value = "" + total.toFixed();
}


HTM:
<input type="checkbox" 
                             name="product" 
                             id="p1" 
                             value="1"
                             onClick="totalIt()"/> 

<input type="checkbox" 
                             name="product" 
                             id="p27" 
                             value="2"
                             onClick="totalIt()"/> 
                  
                       

Total Risk Factor Score &nbsp;&nbsp;
                <input type="text" 
                      name="txtrisk" 
                      id="total" 
                      value="0" 
                      maxLength = 120
Any suggestions  would be appreciated!
Thank you,
Reply all
Reply to author
Forward
0 new messages