Home > DeveloperSection > Forums > sum input values and skip one of them jquery
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    May-18-2013 1:28 AM

 JQuery JQuery 
Ratings:


 1 Reply(s)
 1038  View(s)
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! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-18-2013 8:33 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick