Cannot read properties of null (reading 'getAttribute')

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
2 min

banner

# Cannot read properties of null (reading 'getAttribute')

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

  1. Calling the getAttribute() 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 getattribute of null

Here is an example of how the error occurs.

index.js
const el = null; // โ›”๏ธ Uncaught TypeError: Cannot read properties of null (reading 'getAttribute') el.getAttribute('id');
The code for this article is available on GitHub

# 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 a non-existent id to the getElementById method.

index.js
const el = document.getElementById('does-not-exist'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot read properties of null (reading 'getAttribute') const id = el.getAttribute('id');
The code for this article is available on GitHub

We passed a non-existent id to the getElementById() method and got a null value back.

Calling the getAttribute() 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 runs before button exists โ›”๏ธ --> <script src="index.js"></script> <div id="box">Test</div> </body> </html>

The JS script tag is placed above the code that declares the div element, so the div element is not accessible from the index.js file.

index.js
const el = document.getElementById('box'); console.log(el); // ๐Ÿ‘‰๏ธ null // โ›”๏ธ Cannot read properties of null (reading 'getAttribute') const id = el.getAttribute('id');
HTML code is parsed from top to bottom, so the script has to be placed after the DOM elements have been declared.

Instead, move the JS script tag to 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 id="box">Test</div> <!-- โœ… GOOD - div already exists โœ… --> <script src="index.js"></script> </body> </html>

move js script tag at bottom of body tag

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

index.js
const el = document.getElementById('box'); console.log(el); // ๐Ÿ‘‰๏ธ div#box // โœ… Works const id = el.getAttribute('id'); console.log(id); // ๐Ÿ‘‰๏ธ box

access getattribute method successfully

The code for this article is available on GitHub
If the attribute doesn't exist, the getAttribute method returns either null or an empty string.

# Conclusion

To solve the "TypeError: Cannot read properties of null (reading 'getAttribute')" error, make sure the id you're using to get the element exists in the DOM.

The error often occurs after providing a non-existent id to the getElementById method.

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