SUM INPUT VALUES AND SKIP ONE OF THEM JQUERY

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
JQuery 
 1353  View(s)
Ratings:
Rate this:
Hi!

I have form which is making sum of numbers but i want that middle input to be skipped by jquery for summing... please help with this code here i posting... it surely 

must be inupt tag because it sends data to php. i spend so much time to figure this out but i cant like you see.

jQuery

    var total;
    $("input.amount").keyup(function() {
        total = 0;
        $(this).parents("table").find("input.amount").each(function() {
            total += parseInt($(this).val());
        }).end().find("span.total").html(total);
    });
HTML

  <form class="calc">
    Form 1
    <br />
    <table border="0">
        <tr>
            <td>
                Amount 1
            </td>
            <td>
                <input class="amount" type="text" />
            </td>
        </tr>
        <tr>
            <td>
                Amount 2
            </td>
            <td>
                <input class="amount" type="text" />
            </td>
        </tr>
        <tr>
            <td>
                Amount 3
            </td>
            <td>
                <input class="amount" type="text" />
            </td>
        </tr>
        <tr>
            <td>
                Total Amount
            </td>
            <td>
                <span class="total">0</span>
            </td>
        </tr>
    </table>
    </form>

Thanks in advance! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: sum input values and skip one of them jquery

    Hi Manoj!


    Simply change the HTML markup to give the input you wish to ignore a new class or data-* attribute.

    <input class="amount ignored" type="text" />
    Then check for the class in your .each() loop:

    if(!$(this).hasClass('ignored'))
        total += parseInt($(this).val());

    If you go down the data-* route, you could use:

    <input class="amount" type="text" data-ignored="true" />

    And check making sure that the data attribute is set:

    if(!$(this).data('ignored'))
    total += parseInt($(this).val());

    I hope it helpful for you.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!