I have a form with multiple textboxes and number of textboxes do change depending on the values I retrieve from the database. What I am trying to do is multiply all values of textfields values so first I put them into an array and multiply them and send the answer to a textbox. But I cant seem to get the right answer.
For example when I have 3 textboxes and I enter 1 to each text box I should get 1x1x1 = 1 but I get answer as 3. An when I enter 2 in each text box I should get 2x2x2 = 8 but answer is 12.
My code is as follows:
Javascript Code:
function sumfrm(form){
var totalOdds = 0;
var odds = document.getElementsByTagName('input');
var n;
var i; //
for (i = 0; i < odds.length; ++i) {
n = parseFloat(odds.item(i).value, 10);
if (!isNaN(n)) {
totalOdds += n*n;
}
}
should do the trick
What you currently have for 1x1x1.
totalOddsin the begining is 0After
totalOdds += 1 * 1is 1
Again after second time
totalOdds += 1 * 1is 2
and after third time it is 3