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.
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 :)
+ 4
Joey.innerHTML=parseInt(op)+5;
+ 1
Thanks Schindlabua :)
+ 1
<body>
<div id="Chandler"> 8</div>
</body>
<script>
var Joey = document.getElementById ("Chandler");
var op=Joey.innerHTML;
Joey.innerHTML=parseInt(op)+5;
</script>
+ 1
Joey.innerHTML=+op+5;