Well i was trying to create a simple calculator using HTMl, JS and JQuery , I created two text box for input of numbers but , I am not able to add the given numeric values in the input box , it always concatenate the values . Please Help Me .
Asked
Active
Viewed 421 times
3
-
What do you mean. can you setup a fiddle? – dchhetri Jun 02 '13 at 01:36
2 Answers
1
Input boxes values' will be strings, and not numbers. To convert that input box to a number, try parseInt()
or parseFloat()
around the values. Then, you can add them with +
.

Brad
- 159,648
- 54
- 349
- 530
1
when you read the values from your input boxes, you are reading them as strings. unfortuneately for javascript & math, the + symbol means two different things (in math it means add, in js is means concatentate, and, well add sometimes too).
You need to cast your strings as ints ( or floats or whatever) and then add them in javascript. you can do that this way:
var intstr0 = "1";
var intstr1 = "2";
var sum = parseInt(intstr0) + parseInt(intstr1);

Tucker
- 7,017
- 9
- 37
- 55
-
-
_If_ you use `parseInt()` you should specify a radix as per Blender's suggestion, but why would you use `parseInt()` at all? The user might enter `"1.7"`. – nnnnnn Jun 02 '13 at 02:07