why doesn't this code work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why doesn't this code work?

<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p class='nm'>1</p> <script type="text/javascript"> function fun(){ var x=document.getElementById('nm'); x+=1;} </script> <button id='nmc' onclick="fun()">add</button> </body> </html>

25th Feb 2020, 12:08 PM
Pattern
Pattern - avatar
1 Answer
+ 2
• you give the p element a classe but you call an id. • you have to get the innerText of the element. • you have to conver the innerText into number (doed by the " *1 "). • after all, you have to push your new value into the innerText. try this: ================== <p id='nm'>1</p> <script type="text/javascript"> function fun(){ document.getElementById('nm').innerText = (document.getElementById('nm').innerText * 1) + 1; } </script> <button id='nmc' onclick="fun()">add</button> =========================
25th Feb 2020, 1:28 PM
Gann
Gann - avatar