Uncaught TypeError: Cannot read property 'innerHTML' of null (solution)
Nikhil Chandra Roy
Posted on May 31, 2021
Recently I develop some static pages and got few pages Uncaught TypeError: Cannot read property 'innerHTML' of null
or similar kind of issues.
Issues coming when something missing like classes, selector in some pages but we are calling each portion of JavaScript to perform all the pages.
<h2 class="">Hello World</h2>
<script>
let demo = document.querySelector('.demo')
console.log(demo.innerHTML)
</script>
issues coming when I am calling demo class but there are no demo class in Dom. So, in this moment javascript throwing TypeError.
but, if we make it short with just logical operators &&
it's not giving any TypeError. For example,
let demo = document.querySelector('.demo')
demo&&(
console.log(demo.innerHTML)
)
Thanks.
💖 💪 🙅 🚩
Nikhil Chandra Roy
Posted on May 31, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
beginners NgSysV2-1.1: A Young Person’s Guide to Systems Development in the Age of AI
November 26, 2024