1

I'm trying to print out a JS data into an HTML DOM. I couldn't get it to print. What did I do wrong ?

<!DOCTYPE html>
<html>

    <head>
        <title>JSON</title>
        <script type="text/javascript">
            // user is the property of obj1
            var obj1 = {
                user: "John",
                age: 26,
                country: "United States"
            };
            document.getElementById("results").innerHTML = obj1.user + "<hr>";
        </script>
    </head>

    <body>
        <div id="results"></div>         <---- Expected to see John HERE
    </body>

</html>

In my console

Uncaught TypeError: Cannot set property 'innerHTML' of null

Here is my JSFiddle

3
  • Read the error console. That should indicate the problem. Work backwards from there. Commented Jun 10, 2015 at 0:41
  • jsfiddle.net/m2y9oqrs/1 Commented Jun 10, 2015 at 0:42
  • I'm very to new to JS. I totally forgot about my console. Thanks for reminding. Commented Jun 10, 2015 at 0:42

3 Answers 3

1

The div with the Id of results has not loaded when the document.getElementById("results").innerHTML = obj1.user + "<hr>"; in the Javascript was called.

So window.onload can be used to only execute the Javascript code when the document is loaded.

<!DOCTYPE html>
    <html>
    
        <head>
            <title>JSON</title>
            <script type="text/javascript">
                // user is the property of obj1
                window.onload = function() {
                    var obj1 = {
                        user: "John",
                        age: 26,
                        country: "United States"
                    };
                    document.getElementById("results").innerHTML = obj1.user + "<hr>";
                }
            </script>
        </head>
    
        <body>
            <div id="results"></div>         <---- Expected to see John HERE
        </body>
    
    </html>

Another way is to put the script tag (without the window.onload, as in the OP's post) behind <div id="results"></div>, so that the div is loaded before the JavaScript code is executed.

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

1 Comment

First to answer, the reward is yours. Thank-you. :)
1

There might be more than 1 way to solve this, but I solve mine by wait for the window to load:

window.onload = function()
{
    // my JSCodeHERE
}

1 Comment

Upvoting because I didn't knew window.onload executes after the page is loaded completely
1

The reason you are getting the error is because you have specified the script within the head section and so the script is executed before the HTML element with id="results" is created

Instead of importing the script in the head section, import it just before the closing body tag </body>

    <body>
        <div id="results"></div>         <---- Expected to see John HERE
        <script type="text/javascript">
            // user is the property of obj1
            var obj1 = {
                user: "John",
                age: 26,
                country: "United States"
            };
            document.getElementById("results").innerHTML = obj1.user + "<hr>";
        </script>
    </body>

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.