Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to get the sum total of check box for each checked row in a table


UPdated: Updated Fiddle
My Code Works when Click indiviually,When i click Select All in S1 (Example),the First and Second text Field Expected to contain 10 ,

What I have tried:

JavaScript
$(document).ready(function()
      {  
        $('input[type="checkbox"]').change(function()
      {
               var total=0;
        $(this).parents("tr").children("td").
          find('input[type=checkbox]:checked').each(function(){ 
                total +=parseInt($(this).val());
        $(this).parents('tr').find('input[type=text]:last').val(total);
      });
      });
      });

Problem is the above code Not sum the row value ,when i Click Select All.Any Help For this issue?
Posted
Updated 9-Dec-20 23:22pm
v5

1 solution

(Cross-posted from StackOverflow)
JavaScript
$(this).parents("tr").children("td").find('input[type=checkbox]:checked')
When you tick the checkall checkbox, it will be matched by this selector. It doesn't have an explicit value set, so .val() will return the string "on".

Obviously, the string "on" cannot be converted to an integer, so your total will become NaN.

Exclude the .checkall checkbox from your selector, and your code will work.
JavaScript
$(this).parents('tr').find(':checkbox:checked').not(".checkall").each(...
Updated fiddle[^]

NB: You should also pay attention to the jQuery documentation[^] - $(document).ready(fn) has been deprecated since v3. Use $(fn) instead.


Edit: Based on your updated Fiddle, you just need to trigger the change event of your checkbox after you've updated it from the SelectAll function:
JavaScript
table.find('td:nth-child(' + columnIndex + ') input').prop("checked", obj.checked).change();
Demo[^]
 
Share this answer
 
v2
Comments
Member 12200805 10-Dec-20 4:58am    
Thank for Your response sir,I am unable to paste the image here..i posted one image in stackoverflow.Kindly take a look.my requirement is SelectAll for Column wise thats why i add children("td")
Richard Deeming 10-Dec-20 5:02am    
Then the sample fiddle you provided, and the question you posted here, do not match your real question.
Member 12200805 10-Dec-20 5:04am    
I am Using PHP in my real Code thats why ,i am unable to create fiddle with my original work.sorry if this is wrong
Richard Deeming 10-Dec-20 5:13am    
It's no use creating a Fiddle that doesn't match the problem you are trying to solve!

Create a sample fiddle using a static table with the same structure as your real table. Include your SelectAll Javascript function. And provide a proper description of the problem.

Remember, we can't see your screen, access your computer, or read your mind. If you don't tell us precisely what problem you are trying to solve, we can't help you to solve it.
Member 12200805 10-Dec-20 5:14am    
Thanks for Your Reply Sir.Sorry,I updated my fiddle

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900