3

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 .

Anurag-Sharma
  • 4,278
  • 5
  • 27
  • 42

2 Answers2

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
  • You should specify a radix: `parseInt(intstr0, 10)` – Blender Jun 02 '13 at 01:45
  • _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