Cannot read properties of null (reading 'classList') in JS

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# Cannot read properties of null (reading 'classList') in JS

There are 2 reasons the "Cannot read properties of null (reading 'classList')" error occurs:

  1. Accessing the classList property on a null value (a DOM element that doesn't exist).
  2. Placing the JS script tag above the HTML where the DOM elements are declared.

cannot read property classlist of null

Here is an example of how the error occurs.

index.js
const box = document.getElementById('does-not-exist'); console.log(box); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Uncaught TypeError: Cannot read properties of null (reading 'classList') box.classList.add('bg-green-400');

We attempted to access the classList property on a null value which caused the error.

# Make sure the DOM element exists before accessing classList

Make sure the id you're using to get the DOM element is valid and exists in the DOM.

The error often occurs when providing an invalid id to the getElementById() method.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- ๐Ÿ‘‡๏ธ id should match with JS code --> <div id="box">Hello</div> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

The id of the DOM element should match the identifier you have passed in the call to the document.getElementById() method.

# Place the JS script tag at the bottom of the body tag

Place the JS script tag at the bottom of the body tag.

The JS script tag should run after the HTML elements have been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ›”๏ธ BAD - script runs before div is created โ›”๏ธ --> <script src="index.js"></script> <div id="box">Hello</div> </body> </html>

The index.js script is run before the HTML elements have been created, so we can't access the HTML elements in the index.js file.

index.js
const box = document.getElementById('box'); console.log(box); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot read properties of null (reading 'classList') box.classList.add('bg-green-400');

Instead, you should place the script tag at the bottom of the body tag, after all DOM elements it needs access to.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box">Hello</div> <!-- โœ… GOOD - div already exists โœ…๏ธ --> <script src="index.js"></script> </body> </html>

place script tag at bottom of body tag

The code for this article is available on GitHub

Now the div with id of box is accessible inside of the index.js file.

index.js
const box = document.getElementById('box'); console.log(box); // ๐Ÿ‘‰๏ธ div#box // โœ… works box.classList.add('bg-green-400');

accessing classlist property successfully

The code for this article is available on GitHub

HTML code is parsed from top to bottom, so we have to place the script tag at the bottom of the body tag, after all of the DOM elements it needs access to.

# Conclusion

The "Cannot read properties of null (reading 'classList')" error occurs when trying to access the classList property on a null value.

To solve the error, make sure the JS script tag is loaded after the HTML is created and the id of the element exists in the DOM.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev