JavaScript innerHTML not working

The element doesn't exist at the time you're attempting to set a value. You need to call this after the <h1> has been added to the DOM.

You can either move this <script> tag down further, or add your logic to a function which ought to be called when the document has been loaded:

window.onload = function() {
    /* Add your logic here */
}

Demo: http://jsfiddle.net/Lr2Hm/


You have 2 choices:

window.onload = function() {
  //your script here
}

or

<body>
    <h1 id="ma">s</h1>
    <script type='text/javascript'>
        //your script here
    </script>
</body>

You have to wait until the DOM has loaded.

window.onload = function(){
    document.getElementById("ma").innerHTML="JavaScript";
}

Tags:

Javascript