Last updated: Mar 3, 2024
Reading timeยท4 min
There are 2 main reasons the "Cannot read properties of null (reading 'style')" error occurs:
style
property on a DOM element that doesn't exist.Here is an example of how the error occurs.
const example = null; // โ๏ธ Uncaught TypeError: Cannot read properties of null (reading 'style') console.log(example.style);
The error most commonly occurs if you use the getElementById()
method and pass
it an id
that is not present in the DOM.
const box = document.getElementById('does-not-exist'); console.log(box); // ๐๏ธ null // โ๏ธ Cannot read properties null (reading 'style') box.style.color = 'blue';
id
to the getElementById
methodMake sure that the DOM element you're accessing the style
property on exists.
In the example, an element with the provided id
doesn't exist in the DOM, so
the getElementById
method returns null
.
style
property on a null
value, the error occurs.Another common reason for getting the error is placing the JS script tag before declaring the DOM elements.
Make sure that the JS script tag is placed at the bottom of the body, after the HTML elements have been declared.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- โ๏ธ BAD - Script is run before HTML is declared โ๏ธ --> <script src="index.js"></script> <div id="box" style="color: green">Hello</div> </body> </html>
We placed the JS script
tag before declaring the div
element, so the
index.js
file is run before the DOM element is created.
This means that the div
will not be accessible in the index.js
file.
const box = document.getElementById('box'); console.log(box); // ๐๏ธ null // โ๏ธ Cannot read properties null (reading 'style') box.style.color = 'blue';
You have to move the JS script
tag to the bottom of the body, after the HTML
elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box" style="color: green">Hello</div> <!-- โ GOOD - Script is run after elements are declared โ --> <script src="index.js"></script> </body> </html>
Now the div element is available in the index.js
file.
const box = document.getElementById('box'); console.log(box); // ๐๏ธ the div element // โ works box.style.color = 'blue';
HTML code is parsed from top to bottom, so the script tag has to be placed at the bottom of the body tag, after all of the DOM elements it needs to access.
There are 2 main reasons the "Cannot read properties of undefined (reading 'style')" error occurs:
style
property on a DOM element that doesn't exist, e.g.
array index out of bounds.Here is an example of how the error occurs.
const arr = []; // โ๏ธ Uncaught TypeError: Cannot read properties of undefined (reading 'style') console.log(arr[0].style)
We accessed an empty array at index 0
and got an undefined
value back.
Then we attempted to access the style
property on an undefined
value which
caused the error.
The error most commonly occurs when using the getElementsByClassName
method
with a class name that is not present in the DOM.
const boxes = document.getElementsByClassName('does-not-exist'); console.log(boxes); // ๐๏ธ [] // โ๏ธ Cannot read properties of undefined (reading 'style') boxes[0].style.color = 'red';
getElementsByClassName
Make sure that the value on which you're accessing the style
property is a
DOM element.
In the code sample, we passed a class name that doesn't exist to the
getElementsByClassName
method.
0
returns undefined
. Accessing the style
property on an undefined
value causes the error.The other common reason the error occurs is placing the JS script tag above the code that declares the DOM elements.
Make sure that the JS script tag is placed at the bottom of the body, after the HTML elements have been declared.
You have to place the JS script tag at the bottom of the body tag, after declaring the HTML elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div class="box" style="color: violet">Hello World</div> <!-- โ ๏ธ GOOD - HTML elements are already available โ --> <script src="index.js"></script> </body> </html>
Now you can access the div
element in the index.js
file.
const boxes = document.getElementsByClassName('box'); console.log(boxes); // ๐๏ธ [div.box] // โ works boxes[0].style.color = 'red';
HTML code is parsed from top to bottom, so the script tag has to be placed at the bottom of the body tag, after all of the DOM elements it needs to access.
If you need to set the style
property on all elements in the collection, use a
for...of
loop.
const boxes = document.getElementsByClassName('box'); for (const box of boxes) { box.style.color = 'red'; }
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
Alternatively, you can convert the collection to an array and use the
Array.forEach()
method.
const boxes = Array.from(document.getElementsByClassName('box')); boxes.forEach(box => { box.style.color = 'red'; });
We used the Array.from()
method to convert the collection of elements to an
array and used the Array.forEach()
method to iterate over the array, setting
styles on each element.
The function we passed to the Array.forEach method gets called with each element in the array.