Search code examples
javascriptjquerycheckboxcountchecked

javascript / jquery to count number of checked checkboxes in current row


I have the below table row:

<table class="authors-list">
      <tr>
         <td style="font-size:10px;"><a class="deleteRow"> <img src="delete2.png" /></a></td>
         <td ><input type="text" id="product1" name="product1" class="rounded"/></td>
         <td ><input type="text" size='5' id="qty1" name="qty1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h09_1" name="h09_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h12_1" name="h12_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h15_1" name="h15_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h18_1" name="h18_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h21_1" name="h21_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h24_1" name="h24_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h27_1" name="h27_1" class="rounded"/></td> 
         <td class="tdcheckbox"><input type="checkbox"  id="h30_1" name="h30_1" class="rounded"/></td> 
         <td><input type="hidden" name="checkedsizes_1" id="checkedsizes_1" value="0"></td>
      </tr>
</table>

What I want to do, is count the number of checked checkboxes on each row. keep in my that I can have a dynamic number of rows. This does not have to be in realtime, just before submitting, the hidden field, checkedsizes_1 needs to be set to the number of checked checkboxes for that line.

there could be 5 lines so it needs to loop through each table row(1,2,3...) and count the checked checkboxes then populate the corresponding input checkedsizes_X with the result.

so my submit is:

<INPUT TYPE="button" VALUE="Continue" onClick="submitFunction()">

and the function called is:

<SCRIPT>
function submitFunction() {
   document.sales_order_details.action="/sales/new_autospread_range";
</SCRIPT>

So I envisage something like:

<SCRIPT>

function submitFunction() {
   countcheckboxperrow();
   document.sales_order_details.action="/sales/new_autospread_range";
</SCRIPT>

    <script>
        countcheckboxperrow(){;
        var checkboxes=0;
        //foreach table row in table class="authors-list" count the checkedcheckboxes starting with h. [id^="h"] h being field name first letter      
        //set the corresponsing input in that field row that starts with 
        //repeat for next row id^="checkedsizes"]       
    </script>

Thanks in advance for the help and let me know if I need to clarify. in summary, need a script that counts the number of checked checkboxes in that row and populates a hidden ffield with this value.

Thanks again.

FIDDLE http://jsfiddle.net/emL6x/2/


Solution

  • I've not tested but something like this with jQuery should do it

    countChecks(){
         $('.author-list tr').each(function(){
             var count = 0;
             var hdn = $(this).find('input[name^="checkedsizes"]');
             count = $(this).find(':checkbox:checked').length;
             hdn.val(count);
         });
    }