Skip to content

Uncaught TypeError: Cannot read property ‘style’ of null at HTMLButtonElement

when trying to press nav-toggle icon to appear the navbar content, i get this error.

            <button id="nav-toggle" class="hamburger-menu">
                <span class="strip"></span>
                <span class="strip"></span>
                <span class="strip"></span>
            <ul class="nav-menu-container">
                <li><a href="#">Home</a></li>
                <li><a href="#">Games</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Forums</a></li>
                <li><a href="#">Contact Us</a></li>

here is my javascript:

        document.getElementById('nav-toggle').addEventListener('click', function () {
        let navMenu = document.getElementById('nav-menu-container'); = (navMenu.offsetParent === null  ? 'block' : 'none');


You should use the id attribute for the ul since you are trying to get it using getElementById.

<ul id="nav-menu-container">