javascript - Adding multiple checkboxes together -


i've gotten code work, wanted know why works when there more 1 checkbox named same thing. want each checkbox have it's own name still add together. appreciated. here code:

function checktotal() {     document.listform.total.value = '';     var sum = 0;     (i=0;i<document.listform.choicea.length;i++) {         if (document.listform.choicea[i].checked) {             sum = sum + parseint(document.listform.choicea[i].value);                    }         if (document.listform.choiceb[i].checked) {             sum = sum + parseint(document.listform.choiceb[i].value);         }     }     document.listform.total.value = sum; }   <form name="listform">     <input type="checkbox" name="choicea" value="2" onchange="checktotal()"/>2<br/>     <input type="checkbox" name="choicea" value="5" onchange="checktotal()"/>5<br/>     <input type="checkbox" name="choiceb" value="10" onchange="checktotal()"/>10<br/>     <input type="checkbox" name="choiceb" value="20" onchange="checktotal()"/>20<br/>     total: <input type="text" size="2" name="total" value="0"/> </form> 

if there 1 checkbox name, javascript binding not array, e.g. not choicea[0] choicea.

you can give each item same class name , use getelementsbyclassname (ie9 or above, ie8 , below require shim), returns array of items (well, htmlcollection actually).


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -