document.getElementById not working / Display

There are two mistakes here,

  1. You need to surround id with " (Double Quotes)
  2. You should put script tag after the dom element is created, else it may execute before the actual dom is created. Even if you keep js code in a separate file, make sure you write it after your dom, just before the body tag ends.

So your code should be like,

<p id="pointsdisplay"></p> 
<script>
    var points = 1000;
    document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>

Working jsFiddle

Leave a Comment