Get table cell values when checkbox is check

Hey guys I just need help with some jquery stuff,

How can I get the sum of cell values that is checked after submitting the form…

here’s the basic html that I have…


 <form>
 <table>
 <tr>
      <th>ADD </th>
      <th>Value </th>
  <tr>
     <td><input type="checkbox" name="check_data"></td>
      <td class="num"> 2 </td>
  </tr>
  <tr>
     <td><input type="checkbox" name="check_data"></td>
      <td class="num"> 3 </td>
  </tr>
 <tr>
     <td><input type="checkbox" name="check_data"></td>
      <td class="num"> 4 </td>
  </tr>
  <tr>
    <input type="submit" name="addValues" class="addValue" >
  </tr>
</table>

</form>

  

I want to get the cell that has a class of num
after I checked the checbox cell

please help me…

the only I can do is get the sum of values of the cell without checking the checkbox, its complicated since Im still learning jquery

$('.addValue').click(function() {
var text = 0;
 $('table td.num').each(function()
{
   text += parseInt($(this).text() );

});
alert(text);
});

any help will be much appreciated :slight_smile:

I’ve never used JQuery, but I suspect the issue is that you are not checking the CHECKED status of the checkboxes - you are just adding the numbers regardless. :slight_smile:

how can I checked the status?any hints

This is what you’ll need. I’ve commented so you can see what’s happening. jQuery is a beautiful thing :wink:


	// Attach a click handler
	$('.addValue').click(tallyValues);
	
	// The function that tallies the checked values
	function tallyValues(){
		
		// Set the amount to start at 0
		var amount = 0;
		
		// Loop through each dom element
		$('table .num').each(function(i, val){
			
			// Find the previous sibling (td) and then find the input inside and see if it's checked
			var checkbox_cell_is_checked = $(this).prev().find('input').is(':checked');
			
			// Is it checked?
			if(checkbox_cell_is_checked){
				amount += parseInt($(this).text())
			}
			
		});
		
		// Output the amount
		alert(amount);
		
	}

www.boedesign.com

omg
thanks! jboesch for the help… :slight_smile: