0

Check my fiddle here JSFiddle. I have different id and class and i want to add the dynamically values.while key up I wanna add values dynamically in the total field

I have 3 inputs with ids InvQty1, InvQty2 and InvQty3 and an input with id Total

$(document).ready(function(){
    $('.InvQty').keyup(function(){

       var val2 =  parseInt($('#InvQty1').val())+parseInt($('#InvQty2').val());
        
        $('#Total').val(val2);
    });
});

Any help really appreciated !

1
  • 1
    Please be more clear in what you want to achieve. Commented Nov 6, 2014 at 10:34

6 Answers 6

2

Check out this JSFiddle Link

$(document).ready(function(){
    $('.InvQty').keyup(function(){

       var val2 = 0;
       $("input[id^='InvQty']").each(function()
       {
           if($(this).val().length != 0)
           {
       val2 = val2 + parseFloat($(this).val())  // need to parse int and float avoiding NaN
           }
    });
        
        $('#Total').val(val2);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is now perfect as it is using the ID of the field values. It could be accepted as answer.
0

Try this : on keyup, iterate all InvQty input and sum up value to put it total. Use parseFloat() to avoid NaN

$(document).ready(function(){
    $('.InvQty').keyup(function(){

       var val2 = 0;
        $('.InvQty').each(function(){
            val2+=(parseFloat($(this).val()) || 0);
        });
        $('#Total').val(val2);
    });
});

DEMO

Comments

0

Try the below code

$(document).ready(function(){

    $('.InvQty').keyup(function(){
       var res = 0 ; 
        $(".InvQty").each(function() {
            res += parseInt($(this).val());

        });

        $('#Total').val(res);
    });
});

Fiddle

Comments

0
$(document).ready(function(){
    $(document).on('keyup', '.InvQty', function(){
       var val2 = 0;
        //each input where id starts with 'InvQty' (id='InvQty*')
        $('input[id^="InvQty"').each(function(i, el){
            //get int val
            var curVal = parseInt( $(el).val() );
            //if empty input, then we calculate this as zero value
            var curVal = (isNaN( curVal ) ? 0 : curVal );
            //summ
            val2 = val2 + curVal;
        });
        //result
        $('#Total').val(val2);
    });
});

https://jsfiddle.net/52g6v9kq/ - demo

1 Comment

works perfect with INTEGER, if you need to process float values - change parseInt() to parseFloat()
0

Here's a solution. You set up the function, then, at every keyup, it gets every element with InvQty class and then get its value and add it to the total. Then it sets it in the total input.

  $('.InvQty').keyup(function(){
        
       var total = 0;
        $('.InvQty').each( function(){
           total += parseFloat($(this).val());
        });
        $('#Total').val(total);
    });

This also work if you add more inputs with InvQty class on it. https://jsfiddle.net/2z4ddh7o/13/

Edit: put parseFloat as suggested by myfunkyside

3 Comments

Your code will produce NaN if I enter any non-numeric value
@BhushanKawadkar - Yeah, what'd you expect?
@MichaelZucchetta - You should remove that + however. And use parseFloat() around the values to be safe
-1

You need to try this :

$(document).ready(function(){
        $('.InvQty').keyup(function(){
            var Total = 0;
            $('.InvQty').each(function(){
                if(parseInt($(this).val()))
                    Total += parseInt($(this).val());
            });
            $('#Total').val(Total);
        });
    });

The NaN value is appearing because parseInt('') return NaN and not 0.

I hope this will help you !

2 Comments

You can use parseInt($(this).val()) || 0 to avoid NaN
@BhushanKawadkar just edited my code, used if(parseInt($(this).val())) which will return null if it's NaN and won't pass by the code..

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.