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">

the script:

(function() {

    var x = document.getElementById("menu-principal");
    document.getElementById("menurwd").onclick = function activeRwd(){
            if(x.className === "menu"){
                x.className += " active";
                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]


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: