I have an unknown count. inputov with one class. I need to take from all the values ​​and sum up and display 1 number at the end. How i did

$( ".input_quantity" ).each(function() { var arr = new Array(); arr.push(Number($(this).val())); var summ =0; for (var i =0; i< arr.length; i++) { summ +=arr[i]; } console.log(summ); }); 

It does not go to summarize, each number and input are displayed separately. Most likely, I chose the wrong counting method - through ".each". Tell me another idea?

    1 answer 1

    each is a cycle. You at each iteration of the cycle are trying to calculate the amount. And in a very strange way.

    Deliver the amount of the announcement to the cycle and then add in the cycle:

     var summ = 0; $(".input_quantity").each(function() { summ += +$(this).val(); }); console.log(summ); 
    • Thank you, fresh look saved! - Pavel8289