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?