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

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
2 min

banner

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

The "Cannot read properties of null (reading 'focus')" error occurs for 2 reasons:

  1. Calling the focus() 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.

cannot read property focus of null

Here is an example of how the error occurs.

index.js
const el = null; // โ›”๏ธ Uncaught TypeError: Cannot read properties of null (reading 'focus') el.focus();

# Make sure the DOM element exists

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 // โ›”๏ธ Uncaught TypeError: Cannot read properties of null (reading 'focus') el.focus();
The code for this article is available on GitHub

We passed an id that doesn't exist to the getElementById method and got a null value back.

Calling the focus() method on a null value causes the error.

# 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 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 is run before input exists โ›”๏ธ --> <script src="index.js"></script> <input type="text" id="first_name" value="John" /> </body> </html>
The code for this article is available on GitHub

The JS script tag is placed above the input element, so the index.js file is run before the input element is present in the DOM.

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

The JS script tag should be placed at the bottom of the body tag, after the elements the file needs access to.

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

place js script tag at bottom of body tag

The code for this article is available on GitHub

Now we have access to the input element in the index.js file.

index.html
const el = document.getElementById('first_name'); console.log(el); // ๐Ÿ‘‰๏ธ input#first_name // โœ… Works el.focus();

using focus method successfully

The code for this article is available on GitHub

HTML code is parsed from top to bottom, so the script tag has to come after the code that declared the DOM elements.

Otherwise, the elements aren't accessible in the index.js file.

# Conclusion

The "Cannot read properties of null (reading 'focus')" error occurs when trying to call the focus() 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.

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