Cannot read property 'querySelectorAll' of Null in JS

avatar

Borislav Hadzhiev

Thu Oct 21 20212 min read

Cannot read property 'querySelectorAll' of Null #

The "Cannot read property 'querySelectorAll' of null" error occurs for 2 reasons:

  1. Calling the querySelectorAll() method on a null value (DOM element that doesn't exist).
  2. Placing the JS script tag above the HTML where the DOM elements are declared.
index.js
const el = null; // โ›”๏ธ Cannot read properties of null (reading 'querySelectorAll') el.querySelectorAll('div.box');

To solve the "Cannot read property 'querySelectorAll' of null" error, make sure the id you're using to get the element exists in the DOM. The error often occurs after providing an invalid id to the getElementById method.

index.js
const el = document.getElementById('does-not-exist'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot read properties of null (reading 'querySelectorAll') el.querySelectorAll('div.box');

We passed a non-existent id to the getElementById method and got a null value back. Calling the querySelectorAll() method on a null value causes the error.

To solve the "Cannot read property 'querySelectorAll' of null" error, place the JS script tag at the bottom of the body tag. The script should run after the DOM elements have been created.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ›”๏ธ BAD - script runs before div exists โ›”๏ธ --> <script src="index.js"></script> <div id="container"> <div class="box">Box 1</div> <div class="box">Box 2</div> </div> </body> </html>

Notice that the JS script tag is placed above the code that declares the div element. This means that the index.js file cannot access the div element.

index.js
const el = document.getElementById('container'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot read properties of null (reading 'querySelectorAll') el.querySelectorAll('div.box');

Instead, you should move the JS script tag to the bottom of the body, after the elements it tries to access.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="container"> <div class="box">Box 1</div> <div class="box">Box 2</div> </div> <!-- โœ… GOOD - div already exists โœ… --> <script src="index.js"></script> </body> </html>

Now our index.js file has access to the DOM elements.

index.js
const el = document.getElementById('container'); console.log(el); // ๐Ÿ‘‰๏ธ div#container // โœ… Works const boxes = el.querySelectorAll('div.box'); console.log(boxes); // ๐Ÿ‘‰๏ธ [div.box, div.box]

Conclusion #

The "Cannot read property 'querySelectorAll' of null" error occurs when trying to call the querySelectorAll method on a null value.

To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.

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