0

I want to sum some values when the user send values from a text field with addition symbol.

After split the values of this field I get always NAN.

I've tried several options (parseInt, parseFloat) as you see in the javascript Code, but can't get it work.

Maybe there is someone who can help:

My html Code:

<input type="text" id="testsum" placeholder="gelieferte CBM" value="5+6+8+2" onblur="CbmErrechnen(this.value)">

My javascript:

function CbmErrechnen () {
            var parts = document.getElementById('testsum').value.split('+', 5);
            var zahl0  = parseInt((parts[0]).innerHTML, 10);
            var zahl1  = parseInt((parts[1]).innerHTML, 10);
            var zahl2  = parseFloat(parts[2]);
            var zahl3  = parseFloat(parts[3]);
            var zahl4  = parseFloat(parts[4]);
        var sum = (zahl0)+(zahl1);
            alert(sum);
            return false;

        }

Maybe there is someone who can help.

qJake
  • 16,821
  • 17
  • 83
  • 135
jupp
  • 3
  • 1

4 Answers4

0
(parts[0]).innerHTML

parts will not have an innerHTML attribute.

function CbmErrechnen () {
        var parts = document.getElementById('testsum').value.split('+', 5);
        var zahl0  = parseInt(parts[0]);
        var zahl1  = parseInt(parts[1]);
        var zahl2  = parseFloat(parts[2]);
        var zahl3  = parseFloat(parts[3]);
        var zahl4  = parseFloat(parts[4]);
    var sum = (zahl0)+(zahl1);
        alert(sum);
        return false;

    }
dave
  • 62,300
  • 5
  • 72
  • 93
0

Edit: expanding as per comment below.

Your parseInt in the example you have pasted in doesn't have an innerHTML attribute as dave has mentioned. In the below code we iterate over each of the values in the array that is generated from the split and sum each of the values (of course this assumes you want to add together all of the values).

var parts = document.getElementById('testsum').value.split('+', 5), result = 0;
  for (var a = 0; a < parts.length;a++) {
    result += parseInt(parts[a]);
  }
  alert(result);

Dave's answer is correct if you want to add "some" of the values, or you could alternatively use something like, replacing x and y with the relevant array positions:

var result = parseInt(parts[x]) + parseInt(parts[y])

Many people recommend not to use eval which some of the other answers have suggested see stackoverflow here

Community
  • 1
  • 1
Jeggs
  • 196
  • 2
  • 6
  • Please always add explanations to answers. They become much more helpful to users of this site. – cfi Apr 02 '14 at 21:32
0

You can simply use:

eval(document.getElementById("testsum").value);

DEMO FIDDLE

Buzinas
  • 11,597
  • 2
  • 36
  • 58
0

I was so surprised about the fast and really much answers.

Thank you all. All of them has and will helped me. Really fantastic.

Haven´t really understand why eval(...) not good to use, because this is mostly helpful for my problem but thank you for the Link where this is answered (@Jeggs).

Because I use this code only locally the performance and secure is not to important for me.

So one more time: very - very Thanks to all

jupp
  • 3
  • 1