Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding of numeric values in Html [duplicate]

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 .

like image 392
user2444327 Avatar asked Sep 24 '26 00:09

user2444327


2 Answers

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);
like image 161
Tucker Avatar answered Sep 26 '26 15:09

Tucker


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 +.

like image 37
Brad Avatar answered Sep 26 '26 15:09

Brad