1

I am trying to create counting function with jquery.

I have multiple sets of inputs with numbers determined, something like:

<input class="price" type="hidden" name="price" value="4">
<input class="count" type="text" name="count" maxlength="4">

I want to create script that will count all inputs, eg.

 (price * count) + (price * count) etc..

So far i came up with this script, which multiplies and displays only one input

$('.count').blur(function() {
    var amount = parseFloat($(this).val());
    var price = $(this).closest('tr').find('.price');
    var result = parseFloat((price).val());
    var money = (amount * result)
    $('.money').text(money);
});
1
  • 1
    Could you add a more complete HTML example - we need to see how your .price and .count elements are grouped in order to iterate over them. Commented May 9, 2015 at 11:30

1 Answer 1

1

You need to use a loop inside your blur handler like

var $counts = $('.count').blur(function () {
    var money = 0;
    $counts.each(function () {
        money += (+this.value * $(this).closest('tr').find('.price').val()) || 0
    });

    $('.money').text(money);
});

Demo: Fiddle

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

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.