0

in multiple input field enter the different numbers how can add automatically in javascript

how can i find sum of different input field in different numbers in javascript.

"the input field is n numbers..:" and this input fields i using the clone function

like this modal

only consider in numbers field not related for text field almost this model note:but my condition is here is different name and id

how can i do ? pleas help me..

Community
  • 1
  • 1

1 Answers1

1

You can have onchange event on all inputs that are needed to be summed

<input class="sum" onchange="add()" />

and can add and place the sum into the the total input like below

function add(){
 total= 0;
 sum =document.getElementsByClassName("sum");
 for(a=0;a<sum.length;a++)
   {
     console.log(sum[a].value);
   total += parseInt(sum[a].value || 0);
  }
  document.getElementById("total").value = total;
}

Code/Demo:

function add(){
 total= 0;
 sum =document.getElementsByClassName("sum");
 for(a=0;a<sum.length;a++)
   {
     console.log(sum[a].value);
   total += parseInt(sum[a].value || 0);
  }
  document.getElementById("total").value = total;
}
One   <input class="sum" onchange="add()" /><br/>
Two  <input class="sum" onchange="add()" /><br/>
Three <input class="sum" onchange="add()" /><br/>
Four  <input class="sum" onchange="add()" /><br/>
Total   <input id="total"  /><br/>
A.B
  • 20,110
  • 3
  • 37
  • 71