2

This is a basic html/javascript code, but I am having issues getting the sum of all fields. (The are 50 fields in the original project, but now I just leave 5)

If the field is blank, it just has to ignore it, and add only those with filled fields.

HTML code:

value1:<input type="text" id="total_1" ><br>
value2:<input type="text" id="total_2" ><br>
value3:<input type="text" id="total_3" ><br>
value4:<input type="text" id="total_4" ><br>
value5:<input type="text" id="total_5" ><br>
total:<input type="text" id="totalresult" >
<button type="button" onclick="getTotal(); return false;">Get total</button>

Javascript:

function getTotal() {
var sum;
for (i = 1; i <=5 ; i++) {
    var total = document.getElementById('total_' + i.toString()).value;
    if (total != '') {
        sum = parseFloat(total) + sum;
        document.getElementById('totalresult').value = sum;
    }

  }

}

I don't know why my code is isn't working.

Here is my Fiddle

Herland Cid
  • 574
  • 1
  • 6
  • 23

5 Answers5

3

The first time your code runs, sum will be undefined.

Initialize

var sum = 0;

Also to make it work in the fiddle, you need to change

the onLoad on the left top to 'No wrap - in '

Parthik Gosar
  • 10,998
  • 3
  • 24
  • 16
1

I don't know why your fiddle can't figure out to make getTotal a global. But your main problem is that sum is undefined as start. This will result in NaN (Not a Number) :

var sum;

sum = 1 + sum; // NaN

....

sum = 1 + undefined; // NaN

sum = 1 + NaN; // NaN

Demo at jsbin.com

You should set sum equal zero at first:

var sum = 0;
for ( ... ) { ...

Working demo as adrianp pointed out: It would probably be more clear if you uploaded the working code to jsbin.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
1

You haven't defined sum variable so javascript takes that as NaN, which means Not a Number. You need to initilaize it to set it right.

  function getTotal() {
            var sum = 0;
    for (i = 1; i <=5 ; i++) {
    var total = document.getElementById('total_' + i.toString()).value;
if(isNaN(total) || total.indexOf(' ') == 1) {
    alert("Please type a number");
    document.getElementById("totalresult").value = "I cant sum alphanumerics";
    return false;
} 

if (total != '') {
    sum = parseFloat(total) + sum;
    document.getElementById('totalresult').value = sum;
}

} }

FIDDLE

defau1t
  • 10,593
  • 2
  • 35
  • 47
1

You need to do two things. 1, initialize sum to zero. 2, check the input values for not being a number.

function getTotal() {
    var sum = 0;
    for (i = 1; i <= 5; i++) {
        var total = document.getElementById('total_' + i).value;
        if (!isNaN(parseFloat(total))) sum = parseFloat(total) + sum;
        document.getElementById('totalresult').value = sum;
    }
}

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
0

Use IsNumeric function to check if the input field has a valid number. See: Validate decimal numbers in JavaScript - IsNumeric()

Here is my suggestion to improve the code inside your for loop:

var elementValue = document.getElementById('total_' + i).value
IsNumeric(elementValue) ? elementValue : elementValue = 0;
Community
  • 1
  • 1
Deepak
  • 1
  • 1