title: Javascript calculate the sum of all input fields with a specific class
body: <p>Okay, i have this code which is working fine to a specific point.</p>
  
  <pre><code>    $(document).ready(function(){
            var val1 = +$(".charge").val();
            var val2 = +$(".fee").val();
            var val3 = +$(".loadrate").val();
            $("#finalrate").val(val1-val2+val3);
      $(".financeinput").keyup.each(function(){
            var val1 = +$(".charge").val();
            var val2 = +$(".fee").val();
            var val3 = +$(".loadrate").val();
            var initial = $("#finalrate").val();
  
            $("#finalrate").val(val1-val2+val3);
  
            var second = $("#finalrate").val();
  
            if(initial &lt; second){
  
            $("#finalrate").removeClass("redardown").addClass("greenarup");
  
            }
            if(initial &gt; second){
  
            $("#finalrate").removeClass("greenarup").addClass("redardown");
  
            }
            if(initial = second){
  
            }
  
      });
  });
  </code></pre>
  
  <p>I have multiple fields with class ".charge" and multiple fields with class ".fee" but the code here is getting only the values of the first fields with that class, while i need all of them to be calculated. I know this must be very easy, but i am fairly new to javascript.</p>