0

1+100=1100 in JavaScript

It always combines two numbers as two strings, despite I've used the parseInt() function. How can I fix the problem? Here's the code #There's something above var a=document.getElementById("a"); var b=document.getElementById("b"); parseInt(a); parseInt(b); document.write(a+b); #There's something below

15th Jan 2017, 11:18 AM
Queven
Queven - avatar
5 Answers
+ 4
This is my trick, multiply each variable by 1 so as to convert them to integer.
15th Jan 2017, 11:36 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 4
What want you retrieve from what type of html element? A value from an attribute ( ie: <input>... ), the text content ( ie: <option>... ), the Html text content? Because actually, even if you parseInt() correctly your variables, you cannot obtain a correct result/behaviour: The function 'getElementById' return an object "HtmlElement' which will be converted to 'NaN' value by parseInt()...
15th Jan 2017, 12:03 PM
visph
visph - avatar
+ 1
parseInt(var1) + parseInt(var2)
15th Jan 2017, 11:30 AM
Jani Sinkkonen
Jani Sinkkonen - avatar
0
visph It's a value of input, I just forgot to type .value when I copied my code to here.
15th Jan 2017, 12:33 PM
Queven
Queven - avatar
0
Thanks for your reply.
15th Jan 2017, 12:33 PM
Queven
Queven - avatar