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
5 odpowiedzi
+ 4
This is my trick, multiply each variable by 1 so as to convert them to integer.
+ 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()...
+ 1
parseInt(var1) + parseInt(var2)
0
visph
It's a value of input, I just forgot to type .value when I copied my code to here.
0
Thanks for your reply.