0

How to automatically sum some text field without clicking button for sum it?

<tbody>
  <tr>
    <td>
      <div class="form-group">
        <label for="exampleFormControlTextarea1">result
          <a href="JavaScript:value();" class="badge badge-primary">SUM</a>
        </label>
        <input type="text" class="form-control" id="add" name="sum4" readonly>
     </div>
   </td>    
 </tr>
 <tr>
   <td>
    <div class="form-group">
     <label for="exampleFormControlTextarea1">Nilai Akhir/100</label>
      <input type="text" class="form-control" id="addme" name="result4" readonly>
    </div>
  </td>
 </tr>
</tbody>

and this is the JavaScript.

function value()
{
    var input1 = document.getElementById("totalindex1").value;
    var input2 = document.getElementById("totalindex2").value;
    var input3 = document.getElementById("totalindex3").value;
    var result = 0;
    var divider = 100;
    var resdiv = 0;
    result += parseInt(input1) + parseInt(input2) + parseInt(input3);
    resdiv = result / divider;
    document.getElementById('add').value = result;
    document.getElementById('addme').value = resdiv;
}

the variable "totalindex1", "totalindex2", and "totalindex3" already automatically sum for every button checked. I want to make sum for each totalindex as final result without clicking button for sum these variable.

1
  • 1
    Call value() in whatever place generates the content of the totalindexX elements.
    – Andreas
    Commented Aug 5, 2020 at 15:46

2 Answers 2

2

Use the input event on every <input> tag

This event is fired every time the value of the <input> element changes.

You can get all the desired <input> tags by adding a common class name

Example:

function calcsum() {
  let sum = 0;
  for (const inputEl of document.getElementsByClassName("suminput")) {
    sum += +inputEl.value;
  }
  document.getElementById("sum").innerText = sum;
}
<input class="suminput" oninput="calcsum()" />
<input class="suminput" oninput="calcsum()" />
<input class="suminput" oninput="calcsum()" />
<div>Sum: <span id="sum"></span></div>
<br />
<div>The following input will not add to the sum</div>
<input />

4
  • thanks for answer, if use getelementbytagname('input'), there is somany tag with 'input'. can i use getelementbyclassname instead of tagname ? Commented Aug 6, 2020 at 11:48
  • Yes you can. getElementsByClassName will get all elements with given class Commented Aug 6, 2020 at 11:53
  • but i have three different input as below : input1, input2 and input3. if use same class name for this input, your code can cover these variable ? and in input field is not fill manually by typing the value, the input value is sum automatically by radio button checked. Commented Aug 6, 2020 at 12:18
  • Yes you can. I changed my example to using classes, and I added a 4th input to show that whatever you put in there doesn't get added to the sum Commented Aug 6, 2020 at 12:24
1

Create an eventlistener for all inputfields vor sum which calls your value-function.

for (let i=1; i<=3; i++) {
    document.getElementById("totalindex"+i).addEventListener('input', value);
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.