0

I have a table of invoice numbers, dates and totals due. I would like the selected total returned to jQuery in a variable. PHP is presently generating the table so if it helps to add unique ID's to the inputs or modify the HTML output, that isn't a problem at all.

I've done some research into it and it seems there are a million ways to achieve what I'm looking for; from concatenating the ID's and Prices in the Input ('5:75.97' style) and then separating them for manipulation, all the way to searching through the HTML for the nearest checkbox and adding the numbers that way. They all seem like they would work; just curious to learn what the ideal solution is.

For example, if Invoice 5 and 12 were selected, a variable would equal 106.94

Any help would be greatly appreciated.

<table>
    <tr>
        <th>Invoice #</th>
        <th>Date</th>
        <th>Balance</th>
    </tr>
    <tr>
        <td><input type="checkbox" name="Invoices[]" value="" /> Invoice 5  </td>
        <td>2015-03-03</td>
        <td>75.97</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="Invoices[]" value="" /> Invoice 8</td>
        <td>2015-03-07</td>
        <td>35.97</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="Invoices[]" value="" /> Invoice 12</td>
        <td>2015-03-01</td>
        <td>30.97</td>
    </tr>
</table>

3 Answers 3

1

Here's how I would do it:

    var totalInvoice;
    $('input').on('change', function(){
        totalInvoice = 0;
        $('input:checked').each(function(){
            totalInvoice += parseFloat($(this).parent().siblings('td:nth-last-child(1)').text());
        });
        console.log(totalInvoice);
    })

Keep in mind that it is better if you specify the input that your referring to by a class or something.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest:

// finding the inputs of type=checkbox, binding a change 
// event-handler:
$('table input[type="checkbox"]').on('change', function() {
  // finding the closest <table>:
  var table = $(this).closest('table'),
    // finding the checked checkboxes inside of that <table>:
    checked = table.find('input[type=checkbox]:checked'),
    // getting the last <td> from the <tr> that contains the
    // checked checkboxes:
    prices = checked.closest('tr').find('td:last-child'),
    // iterating over those <td> elements:
    sum = prices.map(function() {
      // creating a map containing the found prices, if the
      // trimmed text is not a number we return 0:
      return parseFloat(this.textContent.trim(), 10) || 0;
    // converting the map to an array, and passing it
    // to the reduce method:
    }).get().reduce(function(a, b) {
      // summing the current value (a) with new value (b):
      return a + b;
    });
  // setting the text of the element whose id="result":
  $('#result').text(sum);
// triggering the change event (in the case of any
// checkboxes being checked on page-load:
}).change();

$('table input[type="checkbox"]').on('change', function() {
  var table = $(this).closest('table'),
    checked = table.find('input[type=checkbox]:checked'),
    prices = checked.closest('tr').find('td:last-child'),
    sum = prices.map(function() {
      return parseFloat(this.textContent.trim(), 10) || 0;
    }).get().reduce(function(a, b) {
      return a + b;
    });
  $('#result').text(sum);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <th>Invoice #</th>
      <th>Date</th>
      <th>Balance</th>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="Invoices[]" value="" />Invoice 5</td>
      <td>2015-03-03</td>
      <td>75.97</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="Invoices[]" value="" />Invoice 8</td>
      <td>2015-03-07</td>
      <td>35.97</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="Invoices[]" value="" />Invoice 12</td>
      <td>2015-03-01</td>
      <td>30.97</td>
    </tr>
  </tbody>
</table>

<div id="result"></div>

References:

Comments

0

You could do something like this: fiddle

var sum = 0;
$('input').click(function () {

    if ($(this).is(':checked')) {
        var price = $('.price');
        var add = ($(this).closest('tr').find(price).html());
        add = parseFloat(add).toFixed(2);
        sum += +add;
        console.log(sum);
    } else if (!$(this).is(':checked')) {
        var price = $('.price');
        var add = ($(this).closest('tr').find(price).html());
        add = parseFloat(add).toFixed(2);
        sum -= +add;
        console.log(sum);
    }

});

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.