Cannot read property ‘className’ [duplicate]

This question already has an answer here:

I’m working on WordPress and Javascript, I want to add a class when you click on the button to show and hide the menu.

This is the html:

<span class="menurwd" id="menurwd"></span>
<ul id="menu-principal" class="menu">
<li>Item</li>
<li>Item</li>
</ul>

the script:

(function() {

    var x = document.getElementById("menu-principal");
    document.getElementById("menurwd").onclick = function activeRwd(){
            if(x.className === "menu"){
                x.className += " active";
            }else{
                x.className = "menu";
            }
        }

})();

In the local installation if it works, but at the time of publishing it generates this error

Read more here: Cannot read property ‘className’ [duplicate]

Leave a Reply

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