1

i have this dynamically populated form fields.

<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>

on jQuery's keyup i want to fetch all the value from input[name="quantity[]"] and perform some calculations like total quantity i.e something like

(field1 quantity value) + (field2 quantity value) + (field3 quantity value) ... n

i tried fetching the value like this.

$('input[name="quantity[]"]').keyup(function(){
    var val = $(this).val();
});

this only fetches the value of first element and ignores the rest of fields. how do i go with what i want?

thank you

4 Answers 4

2

try this:

 <input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>
<input type="text" name="quantity[]" placeholder="quantity"/>

<input type="text" name="res" placeholder="Resultado"/>



$(document).ready(function() {


$('input[name="quantity[]"]').keyup(function(){
   var val = 0;
   $('input[name="quantity[]"]').each(function() {
                val += Number($(this).val());
            });
    $('input[name="res"]').val(val);
});

});

fuente con jsfiddle

http://jsfiddle.net/kSw4G/3/

by JNE

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

Comments

2
var $quantity = $('input[name="quantity[]"]');

$quantity.keyup(function(){
    var val = 0;
    $quantity.each(function(){
        val += parseInt($(this).val(), 10);
    });
});

3 Comments

just out of curiosity, what is the use of second argument in parseInt()?
Just to insure that you are adding numeric values rather than concatenating strings. The + operator does double duty in JavaScript, and .val() returns a string. It is necessary to convert this to a numeric value if you want to math operations.
does not work for the second field, this only fetches the value from first field
1
var ref = $('input[name^="quantity"]');
    ref.keyup(function(e){
        var result = 0;
        ref.each(function() {
          console.log($(this).val());
          if($(this).val().length && $(this).val() != '') {
            result += parseInt($(this).val());
          }
          console.log(result);
        });
    });

Live Demo

7 Comments

since the value is being fetched as string i am getting an error NaN, how do i force jQuery to treat it as integer?
noticed that parstInt already does that. then how come i am getting NAN error
you can do it like this if(val == ""){result += 0}else{result += parseInt(val);}
this have some logical error. if i change the field value then it will keep appending from the last value it fetched. for example if i have value of 100 in field 1 and then if i change it to 200 it will result in 300
still the calculation does not come proper
|
0

A simple solution is to assign each text box a class let suppose quantity then

var total_quantity = 0;  
$('.quantity').keyup(function()
{
    $(this).each(function()
    {
        total_quantity += $(this).val();
    });
}); 

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.