Menu close reads target as Undefined

Asked

Viewed 35 times

0

I am mounting a simple menu that ends when clicking on "Document", however, it appears that the target property cannot be read to Undefined

"Uncaught TypeError: Cannot read property 'target' of undefined"


//Vars
var userMenu = document.querySelector(".menu-holder");
var navButton = document.querySelector("#menu-nav-button");
var doc = document.documentElement;
//Functions
function toggleMenu(event){

    if(navButton.classList.contains("active")){
        navButton.classList.remove("active");
        userMenu.classList.remove("active");
        doc.classList.remove("active");
    }else{
        navButton.classList.add("active");
        userMenu.classList.add("active");
        doc.classList.add("active");
    }
}
function closeMenu(event){
    if(event.target == document.documentElement){
        navButton.classList.remove("active");
        userMenu.classList.remove("active");
        doc.classList.remove("active");
        console.log("oloco")
    }else{
        console.log("deu ruim")
    }
}closeMenu();

How can I correct the mistake, and why it happens ?

  • 2

    Colleague, Voce is not passing any parameters to the "closeMenu" function and she is expecting some. : Function closeMenu(Event) ... closeMenu(semEvent);

  • What do you mean ? I didn’t understand very well

2 answers

0

Its function called closeMenuwas to receive some parameter : function closeMenu(event){. This (Event) is the parameter.

And you call the function without passing any value to it : }closeMenu();

Try passing some value to this function.

  • I passed the Event value even but it didn’t spin

0


I checked the Script above, the same if solved just by removing the statement CloseMenu() at the end of the creation of the

Browser other questions tagged

You are not signed in. Login or sign up in order to post.