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.