This is my javascript code:
var num1;
function press_Button(){
num1 = 3;
}
Then I have a button with this: onclick="press_Button()"
and in the HTML is this:
<script type="text/javascript">
document.write(num1);
</script>
The page displays the variable as undefined, even after hitting the button, and I can’t figure out why.
Help!
You’ll need to update the display of num1 after you call
press_Button. What I would do is ditch the document.write, and instead make a span:And then:
Or, if you need this to work on ancient browsers that don’t like textContent, you could do: