4

I have many input fields. when i typing in one field it multiplier with a number. how can i make sum of all fields are on the end?

$("#100").keyup(function(){
 $("#100_").val(this.value*100.00);
});

$("#50").keyup(function(){
    $("#50_").val(this.value*50.00);
});
$("#20").keyup(function(){
    $("#20_").val(this.value*20.00);
});
$("#10").keyup(function(){
    $("#10_").val(this.value*10.00);
});
$("#5").keyup(function(){
    $("#5_").val(this.value*5.00);
});
$("#2").keyup(function(){
    $("#2_").val(this.value*2.00);
});
$("#1").keyup(function(){
    $("#1_").val(this.value*1.00);
});

that is my code for multiplier with number. it works. but at the end i have a input field and i want to sum all input fields wit id "*_"

1
  • Your question is not quite clear. Where is your script which sum all the multiplier fields? Is this what you want? Commented Jun 4, 2016 at 2:02

1 Answer 1

2

The strategy: Use also class attribute to help you, for example, define a class moneyInput for each input(#20_, #50_, etc.).

Then, use the following statement:

var c,sum=0;
$(".moneyInput").on("input",function(){ 
   $("#"+this.id+"_").val(this.value*parseInt(this.id));
  $(".moneyInput").each(function(){
    sum+=parseInt($(this).val());
  });
  $("#total").val(sum);
});
// the sum will store what you want, I think?

And delete all your events which is previously defined.

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

6 Comments

thanks but how can i write this sum in a text field at the end?
What! Just output it!
how can i do that output in my text field #sum live? i have testet all ways.
What do you mean by live? You mean whenever users input the one, do the sum? See my edits
yes that i want. at end is the input #sum and when a user type a number in #5 #2 ... do the sum of #5_ #2_ ... in input #sum
|

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.