14

I want to create a calculator which simply sums 2 fields up. But whatever I try it does not work. It also returns "NaN", also if I use parseInt().

Here's the code:

 <script type="text/javascript" language="Javascript">
 function doSum() 
 {
 var a = document.getElementsByName("a").value;
 var b = document.getElementsByName("b").value;

 var sum =  a + b;

 document.getElementById("sum").value = sum;
}
</script>

<form action="" method="POST">
<br/>a:<br/>
<input type="text" name="a" onblur='doSum()' value="0" size="5" />
<br/>b:<br/>
<input type="text" name="b" onblur='doSum()' value="0" size="5" />
<br/>Ergebnis<br/>
<input type="text" id='sum' value='' size="50" disabled/>
</form>

Sorry for that noob question, but what I'am doing wrong? Thanks for any help!

8 Answers 8

16

Give ids to your inputs and identify them uniquely using document.getElementById. Then, obtain their decimal int values using parseInt with the radix parameter set to 10 and display the result as you currently do.

<script type="text/javascript" language="Javascript">
 function doSum() 
 {
  var a = parseInt(document.getElementById("a").value, 10);
  var b = parseInt(document.getElementById("b").value, 10);

  var sum =  a + b;

  document.getElementById("sum").value = sum;
}
</script>

<form action="" method="POST">
 <br/>a:<br/>
<input type="text" id="a" onblur='doSum()' value="0" size="5" />
 <br/>b:<br/>
<input type="text" id="b" onblur='doSum()' value="0" size="5" />
 <br/>Ergebnis<br/>
<input type="text" id='sum' value='' size="50" disabled/>
</form>

getElementsByName returns a list of elements and you'd have to refer to the one you want through an index, even if the list contains only one element.

getElementById on the other hand, returns an uniquely identified element, by its id.

Sign up to request clarification or add additional context in comments.

2 Comments

You should be using the radix parameter of parseInt() to prevent unexpected results ("08" etc).
How do I check if the value is a number or a string. I want to be able to do something different if I have a string as opposed to a number. I don't want the program to quit if it's not a number but I can't seem to get it to work. Is it because what's returned from the input is a string and has to changed to number value?
2

use getElementById and give each of those an Id. getElementsByName returns an array. By the way.. it's not a bad question. It's a common error-- one that is addressed in a way by using jQuery which deals in wrapped sets.

2 Comments

getElementsByName does not return an array; it returns a NodeList.
getElementsByName returns a NodeList, which superficially looks like an array but you can't actually perform Array methods such as push, join, slice, etc
2

getElementsByTagName returns a node list:

 function doSum() 
 {
     var a = document.getElementsByName("a")[0].value;
     var b = document.getElementsByName("b")[0].value;

     var sum =  parseInt(a, 10) + parseInt(b, 10);

     document.getElementById("sum").value = sum;
}

So you will need to index it. In addition in order not to do a string concate, parseInt with radix 10 is needed. Unless you plan to accept octal values in your calculator.

Comments

2

getElementsByName returns multiple elements, hence the plural Elements. You need to get the property of the first element found:

var a = document.getElementsByName('a')[0].value;

getElementsByName returns a NodeList: this is a set of all the elements found with that name. It is like an array in that you can use numeric indexes (like [0]) to access the elements found and in that there is a length property; no other array-like functionality is available.


Furthermore, the value property will always be a string if it is set. The + operator is the addition operator when the values are numbers; if they are strings, it is the concatenation operator. "1" + "2" is equal to "12" in Javascript. You need to use parseInt to convert them to numbers:

var a = document.getElementsByName('a')[0].value;
a = parseInt(a, 10); // parse as a number in base 10

Comments

2

Fields in JavaScript are all strings you need int, also .getElementsByName returns an array, you probably need the first element, so:

var a = parseInt(document.getElementsByName("a")[0].value, 10);
var b = parseInt(document.getElementsByName("b")[0].value, 10);

1 Comment

Since a and b are coming from untrusted sources you should probably include a radix parameter with the parseInt... otherwise if the user enters 08 as a value for a and 1 for b you'll get 1 as the result.
1

getElementsByName returns an array which gives you the wrong data type for what you are trying to do.

try:

function doSum() 
{
  var a = document.getElementById("a").value;
  var b = document.getElementById("b").value;

  var sum =  a + b;

  document.getElementById("sum").value = sum;

}
</script>

<form action="" method="POST">
<br/>a:<br/>
<input id="a" type="text" name="a" onblur='doSum()' value="0" size="5" />
<br/>b:<br/>
<input id="b" type="text" name="b" onblur='doSum()' value="0" size="5" />
<br/>Ergebnis<br/>
<input type="text" id='sum' value='' size="50" disabled/>
</form>

Comments

1

OK, two issues, your a fetching the valurs of a and b using getElementsByName which returns an array of values (since there could be many). Use getElementsById and put ids in the HTML.

Also the value properties will be strings so you will need to convert to numbers before doing your addition.

Comments

0

a and b are strings so :

 function doSum() 
 {
 var a = parseInt(document.getElementsByName("a").value);
 var b = parseInt(document.getElementsByName("b").value);

 var sum =  a + b;

 document.getElementById("sum").value = sum;
}

1 Comment

This will not work because getElementsByName returns a collection of elements. Also, the poster said they tried parseInt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.