javascript - How to get sum of the calculated column on input -
i have tree number columns, , create calculated column sum of tree inputs , have click save see change . i'm looking result on calculated column ,when type or change number in 1 of 3 column before pressing save. tried javascript code :
function myfunction() { var x = document.getelementbyid("myinput").value; document.getelementbyid("demo").innerhtml = "you wrote: " + x; }
but in case have 3 number columns idea?
see solution below. you'll have add validation numbers allowed
var inputs = document.getelementsbyclassname("myinput"); function myfunction() { var total = 0; array.prototype.foreach.call(inputs, function (input) { total += +input.value; }); document.getelementsbyclassname("myoutput")[0].innerhtml = "the total is: " + total; } (var = 0; < inputs.length; i++) { inputs[i].addeventlistener("input", function () { myfunction(); }); }
<form action="#"> <input type="text" class="myinput"> <input type="text" class="myinput"> <input type="text" class="myinput"> </form> <div class="myoutput"></div>
what loop through inputs while attaching event listener , calculating total. there 2 ways this, both implemented above.
Comments
Post a Comment