admin管理员组

文章数量:1427140

I'm building an app with ES5 JS just for practice and "fun" where I store websites in localStorage then print them out on the page, i.e. a bookmarker application.

I'm getting a

TypeError: Cannot set property 'innerHTML' of null

error in the console when I run the following code:

index.html

  <body onload="fetchBookmarks()">

    <div class="container">
        ...some code
      </div>

      <div class="jumbotron">
        <h2>Bookmark Your Favorite Sites</h2>
        <form id="myForm">
            ...some code
        </form>
      </div>

      <div class="row marketing">
        <div class="col-lg-12">
            <div id="bookmarksResults"></div> /* problem code */
        </div>
      </div>

      <footer class="footer">
        <p>&copy; 2018 Bookmarker</p>
      </footer>

    </div>
    <link rel="stylesheet" href="main.css">
    <script src="./index.js"></script>
</body>

index.js

...someJScode that stores the websites in localStorage
function fetchBookmarks() {
    var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));

    //Get output id
    var bookmarksResults = document.getElementById('#bookmarksResults');

    bookmarksResults.innerHTML = '';
    for(var i = 0; i < bookmarks.length; i++) {
        var name = bookmarks[i].name;
        var url = bookmarks[i].url;

        bookmarksResults.innerHTML += name;
    }

}

now, the error is obviously because I am loading the <body> before the <div id="bookmarksResults"></div> so innerHTML responds with null

But two things here:

1) When I assign onload="fetchBookmarks()" to the <footer> element, the function doesn't run. 2) The tututorial I am following has this code almost exactly and it runs there.

I've also tried running the fetchBookmarks() function like this:

 window.onload = function() {
            fetchBookmarks();
            function fetchBookmarks(){
                ...some JS code
            };
        }

But that returned the same

TypeError: Cannot set property 'innerHTML' of null

So I'm a bit lost here and am much more interested in figuring out why this isn't working and the theory behind it so I understand JS better (the whole point of building this app in the first place).

Any help would be appreciated! Thanks SO team.

I'm building an app with ES5 JS just for practice and "fun" where I store websites in localStorage then print them out on the page, i.e. a bookmarker application.

I'm getting a

TypeError: Cannot set property 'innerHTML' of null

error in the console when I run the following code:

index.html

  <body onload="fetchBookmarks()">

    <div class="container">
        ...some code
      </div>

      <div class="jumbotron">
        <h2>Bookmark Your Favorite Sites</h2>
        <form id="myForm">
            ...some code
        </form>
      </div>

      <div class="row marketing">
        <div class="col-lg-12">
            <div id="bookmarksResults"></div> /* problem code */
        </div>
      </div>

      <footer class="footer">
        <p>&copy; 2018 Bookmarker</p>
      </footer>

    </div>
    <link rel="stylesheet" href="main.css">
    <script src="./index.js"></script>
</body>

index.js

...someJScode that stores the websites in localStorage
function fetchBookmarks() {
    var bookmarks = JSON.parse(localStorage.getItem('bookmarks'));

    //Get output id
    var bookmarksResults = document.getElementById('#bookmarksResults');

    bookmarksResults.innerHTML = '';
    for(var i = 0; i < bookmarks.length; i++) {
        var name = bookmarks[i].name;
        var url = bookmarks[i].url;

        bookmarksResults.innerHTML += name;
    }

}

now, the error is obviously because I am loading the <body> before the <div id="bookmarksResults"></div> so innerHTML responds with null

But two things here:

1) When I assign onload="fetchBookmarks()" to the <footer> element, the function doesn't run. 2) The tututorial I am following has this code almost exactly and it runs there.

I've also tried running the fetchBookmarks() function like this:

 window.onload = function() {
            fetchBookmarks();
            function fetchBookmarks(){
                ...some JS code
            };
        }

But that returned the same

TypeError: Cannot set property 'innerHTML' of null

So I'm a bit lost here and am much more interested in figuring out why this isn't working and the theory behind it so I understand JS better (the whole point of building this app in the first place).

Any help would be appreciated! Thanks SO team.

Share Improve this question asked Jun 11, 2018 at 15:37 logos_164logos_164 7861 gold badge14 silver badges33 bronze badges 1
  • Notice the of in Cannot set property 'innerHTML' of null. means that you are trying to access null.innerHTML, not that innerHTML is returningnull. – Jorge Fuentes González Commented Jun 11, 2018 at 15:42
Add a ment  | 

2 Answers 2

Reset to default 6

The problem is with this line:

document.getElementById('#bookmarksResults')

You don't need to prefix the ID with # when you're using it with document.getElementById. Either you may remove the # from the method call, or use document.querySelector(), which works the same way, but support CSS-like selectors to select elements from DOM.

document.getElementById('bookmarksResults');
// OR
document.querySelector('#bookmarksResults');

You need to pass the value of the id without the #

Update from

var bookmarksResults = document.getElementById('#bookmarksResults');

to

var bookmarksResults = document.getElementById('bookmarksResults');

本文标签: javascriptworkaround for innerHTML39null39 when window loadsStack Overflow