Menu closure reads target as undefined

0

I'm setting up a simple menu that closes when clicking on the document, however, it appears that the target property can not 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 error, and why does it happen?

    
asked by anonymous 01.06.2017 / 19:00

2 answers

0

I checked the script above, it was resolved only by removing the CloseMenu() statement at the end of the function creation

    
29.11.2018 / 14:50
0

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

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

Try to pass some value to this function.

    
01.06.2017 / 21:09