Need a little help at JS. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need a little help at JS.

Hi guys, there's something at which i need help... <body> <div id="Chandler"> 8</div> </body> <script> var Joey = document.getElementById ("Chandler"); var op=Joey.innerHTML; Joey.innerHTML=op+5; </script> Now, instead of adding 8 and 5 and giving the result 13, the code gives 85. Any solution by which I can add 8 or any number with 5? Thanks for answering.

30th Sep 2017, 4:45 PM
Isair Calhawk
Isair Calhawk - avatar
5 Answers
+ 7
var op = parseInt(Joey.innerHTML, 10); "innerHTML" returns text (a string), so the 5 you added was converted to text, too. Converting op to a number like I did will work :)
30th Sep 2017, 4:48 PM
Schindlabua
Schindlabua - avatar
+ 4
Joey.innerHTML=parseInt(op)+5;
30th Sep 2017, 5:01 PM
Daniel
Daniel - avatar
+ 1
Thanks Schindlabua :)
30th Sep 2017, 5:02 PM
Isair Calhawk
Isair Calhawk - avatar
+ 1
<body> <div id="Chandler"> 8</div> </body> <script> var Joey = document.getElementById ("Chandler"); var op=Joey.innerHTML; Joey.innerHTML=parseInt(op)+5; </script>
30th Sep 2017, 5:14 PM
Rishita
Rishita - avatar
+ 1
Joey.innerHTML=+op+5;
30th Sep 2017, 7:38 PM
Вадим Сухотин (Vadim Sukhotin)
Вадим Сухотин (Vadim Sukhotin) - avatar