Cannot read property 'querySelector' of Undefined in JS

avatar

Borislav Hadzhiev

Thu Oct 21 20212 min read

Cannot read property 'querySelector' of Undefined #

The "Cannot read property 'querySelector' of undefined" error occurs for 2 reasons:

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

To solve the "Cannot read property 'querySelector' of undefined" error, make sure you're calling the method on a valid DOM element. The error often occurs after passing an invalid class name to the getElementsByClassName method.

index.js
const elements = document.getElementsByClassName('does-not-exist'); console.log(elements); // ๐Ÿ‘‰๏ธ [] // โ›”๏ธ Cannot read properties of undefined (reading 'querySelector') const box = elements[0].querySelector('#box');

We passed a non-existent class name to the getElementsByClassName method, so we got an empty array-like object back.

Trying to access an array at an index out of bounds, returns an undefined value and calling the querySelector method on an undefined value causes the method.

To solve the "Cannot read property 'querySelector' of undefined" error, insert 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 is created โ›”๏ธ --> <script src="index.js"></script> <div class="container"> <div id="box">Text</div> </div> </body> </html>

We placed the JS script tag above the code that declares the DOM elements. This means that the div element won't be accessible inside the index.js file.

index.js
const elements = document.getElementsByClassName('container'); console.log(elements); // ๐Ÿ‘‰๏ธ [] // โ›”๏ธ Cannot read properties of undefined (reading 'querySelector') const box = elements[0].querySelector('#box');

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

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

Now we can access the div element inside of the index.js file.

index.js
const elements = document.getElementsByClassName('container'); console.log(elements); // ๐Ÿ‘‰๏ธ [div.container] // โœ… Works const box = elements[0].querySelector('#box'); console.log(box); // ๐Ÿ‘‰๏ธ div#box

Conclusion #

The "Cannot read property 'querySelector' of undefined" error occurs when trying to call the querySelector method on an undefined 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