0

I have a table. Each row has a checkbox and a number in it. The number is within a span with a class.

If the checkbox is checked, I want the number to be added up with all of the other numbers in checked rows. Here is what the row looks like.

 <td><input class="box" type="checkbox" name="selclips[]" value="1234"></td><td><span class="amount">45.00</span></td>
 <td><input class="box" type="checkbox" name="selclips[]" value="1235"></td><td><span class="amount">65.00</span></td>

Then at the bottom I have:

 <td>Total</td><td><div id="total"></div></td>

So, need some help with the JS or jquery to add up those dollar amounts with the span tags and provide a total if the checkbox is checked in that row? So, in the example above, if both boxes were checked, the script would add up 45.00 and 65.00 and put the total of 110 into the Total div. Also, if I un-check the boxes, the amount should change as well.

Thanks very much for any help anyone can give. I really appreciate it!

2
  • 1
    do you realy want to use js? because those scripts could be manipulated... and its really easy to make it on server side Commented Dec 22, 2013 at 7:19
  • Thanks everyone for your answers! Commented Dec 23, 2013 at 7:00

5 Answers 5

2

You need something like

Live Demo

$('.box').change(function(){
   var total = 0;
   $('.box:checked').each(function(){
        total+=parseFloat($(this).parent().next('td').find('.amount').text());
   });
   $('#total').text(total);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Parsing every now and then will increase the script run time and is memory hog.. Which I would not recommend. Instead try my solution.
Hi! Thanks very much for the comments. The issue here is that you are adding up the value of the checkboxes, when I need to add up the Amount within the span. So in the case above, the total should be 110 (45+65). I edited the question to clarify this.
Adil. Thanks very much for your help on this. Almost there. I didn't realize it was relevant, but apparently it is. In my actual code, there are <td>s in between the checkbox and the amount field, so finding the 'next' td isn't going to work. I updated the JSFiddle to reflect this if you wouldn't mind checking it out. Very much appreciate your help.
1

Fairly simple..

$(function(){

    $( ".box" ).on( "click", function(){
        console.log($(this).next().html());
        var total = 0;
        $('.box:checked').each(function(){
         total += parseFloat($(this).next().html());
        });
        $("#total").text('Total is : ' + total);
    });

});

This works..

http://jsfiddle.net/hqR2r/2/

Comments

1

Try this:

 var total = 0;
$('.box').click(function(){
    if($(this).prop('checked')){
      total += Number($(this).closest('tr').children('td:last-child').text());
  }
    else{
      total -= Number($(this).closest('tr').children('td:last-child').text());
    }
  $('#total').text(total);
});

Here is JSFIDDLE

2 Comments

Hi, thanks for the answer! I don't think I was quite clear enough on the question. I edited it for clarity. Thanks!
Sorry! I have something to do. So now I come back. I have just updated my answer. Please try it! Thanks.
0

This perhaps?

var total = 0,
    $totalEl = $('.total');

$('.box:checkbox').click(function () {
    total += (this.checked? 1 : -1) * +this.parent('td').next().children('.amount').text();
    $totalEl.text(total);
});

Comments

0
var total =  Array.prototype.slice.call($('input.box:checkbox:checked'), 0) // changes jQuery object to array of DOM elements
    .reduce(function(a, b) { // calls reduce method
        return a + Number(b.value);
    }, 0);

Simple and fast.

reduce method documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.