0

I already fail at a seemingly basic task in javascript - changing a div's content by a script. Please help me understand where I am going wrong. Here is a minimal example that's not working for me:

<!DOCTYPE html>
<html>
    <head>
        <script>
            document.getElementById('id').innerHTML = "test";
        </script>

    </head>

    <body>
        <div id="id" >blub</div>
    </body>
</html>
1
  • 3
    move the <script> section after your div, or call it on window.load event Commented Mar 11, 2016 at 10:14

7 Answers 7

2

The Problem with you code it that the JavaScript code is executed before the div part is loaded fully. To fix this change you code to:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function changeDiv()
            {
                document.getElementById('id').innerHTML = "test";
            }
        </script>

    </head>

    <body onload="changeDiv();">
        <div id="id" >blub</div>
    </body>
</html>

The onload event will only fire when the html is fully loaded.

Sign up to request clarification or add additional context in comments.

Comments

1

HTML files load starting at the top to the bottom of the html.

In your example, the javascript executes before the <body> tag exists in the DOM.

Moving the javascript to below the relevant html makes it work as you would expect.

Another option would be to keep the script inside the <head> but delay it from executing until the DOM is loaded

window.onload = function() {
     document.getElementById('id').innerHTML = "test";
};

https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload

<!DOCTYPE html>
<html>
    <head></head>

    <body>
        <div id="id" >blub</div>

    <script>
      document.getElementById('id').innerHTML = "test";
    </script>
  </body>
</html>

Comments

1
<!DOCTYPE html>
 <html>
   <head>


   </head>

    <body>
       <div id="id" >blub</div>
       <script>
          document.getElementById('id').innerHTML = "test";
      </script>
   </body>
 </html>

put your script after the div.

Comments

0

You need to use window.onload to call your code when html is fully loaded:

<!DOCTYPE html>
<html>
    <head>
        <script>
            window.onload = function() {
                document.getElementById('id').innerHTML = "test";
            };
        </script>

    </head>

    <body>
        <div id="id" >blub</div>
    </body>
</html>

Comments

0

Your Dom is no not loaded.. Place your script tag at the end before

Comments

0

Just add the script tag before closing the body. It'll work.

Comments

0

You have to write the Javascript after the div-Tag. First the DOM needs to be loaded and after you can change the content inside the DIV.

Place your Javascript before the closing-body Tag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.