Evaluating specific array value using jQuery or JavaScript -
i have 160 inputs in page. want add every 8 inputs , output elsewhere. instead of manually using
var total1 = inputvals[0] + inputvals[1] + inputvals[2] + inputvals[3] + inputvals[4] + inputvals[5] + inputvals[6] + inputvals[7] + inputvals[8];
i want function can specify starting , ending index addarray(9, 17) , add values between , return it. prefer function in javascript jquery ok.
suppose have <input>
elements defined as:
<input type="text" class="myinput" /> <input type="text" class="myinput" /> <input type="text" class="myinput" /> ...
you can create method job of jquery:
function add(start, end) { var total = 0; $(".myinput").slice(start, end).each(function() { total += +this.value; }); return total; }
the same method in pure javascript like:
function add(start, end) { var total = 0, inputs = document.getelementsbyclassname("myinput"); (var = start; < math.min(end, inputs.length); i++) [ total += +inputs[i].value; }); return total; }
Comments
Post a Comment