Solve - Cannot set property 'innerHTML' of Null in JS

avatar

Borislav Hadzhiev

Tue Oct 19 20212 min read

banner

Photo by David Beale

Solve - Cannot set property 'innerHTML' of Null in JavaScript #

The "Cannot set property 'innerHTML' of null" error occurs for 2 reasons:

  1. Setting the innerHTML property on a null value (DOM element that doesn't exist).
  2. Inserting the JS script tag above the HTML, where the DOM elements are declared.

cannot set property innerhtml of null

Here's an example of how the error occurs.

index.js
const el = null; // โ›”๏ธ Cannot set properties of null (setting 'innerHTML') el.innerHTML = 'new value';

To resolve the "Cannot set property 'innerHTML' of null" error, make sure that the DOM element you're setting the innerHTML property on exists. The error most commonly occurs if you use the getElementById() method and pass it an id that is not present in the DOM.

index.js
const el = document.getElementById('does-not-exist'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot set properties of null (setting 'innerHTML') el.innerHTML = 'new value';

In the example, an element with the provided id does not exist in the DOM, so the getElementById method returns a null value. When we try to set the innerHTML property on a null value, the error gets thrown.

Another common reason for getting the error is placing the JS script tag before declaring the DOM elements.

To solve the "Cannot set property 'innerHTML' of null" error, make sure that the JS script tag is placed at the bottom of the body, after the HTML elements have been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ›”๏ธ BAD - script is ran before span exists โ›”๏ธ --> <script src="index.js"></script> <span id="name">John</span> </body> </html>

The JS script tag is added above the code that declared the span element. The index.js file is ran before the span element is created, therefore we can't access the span from the index.js file.

index.js
const el = document.getElementById('name'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot set properties of null (setting 'innerHTML') el.innerHTML = 'Alice';

Instead, the JS script tag has to be moved below the DOM elements that it tries to access.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <span id="name">John</span> <!-- โœ… GOOD - span already exists โœ… --> <script src="index.js"></script> </body> </html>

Now, the span element can be accessed from the index.js file.

index.js
const el = document.getElementById('name'); console.log(el); // ๐Ÿ‘‰๏ธ null // โœ… Works el.innerHTML = 'Alice';

Now that the HTML element is created before the index.js script is ran, we are able to access the DOM element and set it's innerHTML property.

Conclusion #

The "Cannot set property 'innerHTML' of null" error occurs when:

  • trying to set the innerHTML property on a null value (a DOM element that doesn't exist)
  • inserting the JS script tag before the DOM elements have been declared

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee