Categories
JavaScript Answers

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

Spread the love

Sometimes, we want to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

In this article, we’ll look at how to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.

For instance, we write

document.getElementById("note").style.display = "block";

to style the display style for the element with ID note.

We should make sure getElementById doesn’t return null.

Conclusion

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *