DEV Community

Nikhil Chandra Roy
Nikhil Chandra Roy

Posted on

1 2

Uncaught TypeError: Cannot read property 'innerHTML' of null (solution)

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>
Enter fullscreen mode Exit fullscreen mode

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)
        )
Enter fullscreen mode Exit fullscreen mode

Thanks.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more