+ 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>
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>
=========================