i have got a div. If i click the show button the div vill be visible. I find a solution to hide div when i click outside the element, but it is not work, what i do badly?
function login_show(){
let y = document.getElementById("login");
if(!y.style.display || y.style.display == "none"){
y.style.display = "block";
}
else{
y.style.display="none";
}}
window.addEventListener("click", function(){
if(!document.getElementById("login").contains(event.target)){
document.getElementById("login").style.display="none";
}
});
You forgot the 'event' parameter in callback function.
If the show button is outside of the div, you should consider it in the callback function, may be like:
if(!document.getElementById("login").contains(event.target) && event.target !== document.getElementById("showButton"))