Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

workaround for innerHTML = 'null' when window loads

Tags:

javascript

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.

like image 707
logos_164 Avatar asked Mar 11 '26 16:03

logos_164


2 Answers

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');
like image 174
31piy Avatar answered Mar 13 '26 07:03

31piy


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

Update from

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

to

var bookmarksResults = document.getElementById('bookmarksResults');
like image 28
Nikhil Aggarwal Avatar answered Mar 13 '26 05:03

Nikhil Aggarwal