I use $(event.target).closest("#divID").length
to hide a div
when the user clicks outside of it but in the case that the div
is visible and I click on a date (datepicker) it won't hide the div
.
Also if I click on a <select>
sometimes it hides it sometimes it doesn't.
Is there a better solution to hide a div when something else is clicked?
Is my implementation wrong?
ps: #log_in
is the login button, #log_in_form
is the form
that I want to hide on outside click and #log_in_container
is the div that contains #log_in
and and #log_in_form
UPDATE: I just noticed that the disappearance isn't the same on windows 10 and linux ubuntu 16.04. On a pc with windows 10 using google chrome the form disappears on the first click i do on a select(thats the desirable functionality) but still doesn't disappear if i choose a date. While on linux ubuntu 16.04 on google chrome it is as i described above (doesn't disappear on date choice and also doesn't disappear on the first click you do on select)
Example based on Andrei's answer with snippet
$(document).on('click', function(e){
if($(e.target).closest('#log_in').is('#log_in'))
{
$('#log_in_form').fadeIn();
}
else if(!$(e.target).closest('#log_in_container').is('#log_in_container'))
{
$('#log_in_form').fadeOut();
}
})
#log_in_container{
display:inline-block;
width:122px;
height:58px;
margin-left:60px;
background-color:gray;
}
#log_in{
display:block;
width:120px;
height:28px;
text-align: center;
border: 1px solid red;
font-size:16px;
background-color:yellow;
vertical-align: top;
}
#log_in_form{
display:none;
position:absolute;
width:120px;
height:28px;
text-align: center;
background-color: green;
border: 1px solid blue;
}
.type{
display:inline-block;
width:120px;
height:30px;
text-align: center;
border: 1px solid red;
font-size:16px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- ~~~~~~~~~~~~~~~Date picker ~~~~~~~~~~~~~~~ -->
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function()
{
$( "#datepicker" ).datepicker();
});
</script>
<title></title>
</head>
<body>
<select class="type">
<option value="null" disabled="disabled" selected="selected"><p style="color:gray;">Select hero</p></option>
<option value="0">Spiderman</option>
<option value="1">Iron man</option>
<option value="2">Deadpool</option>
</select>
<select class="type">
<option value="null" disabled="disabled" selected="selected"><p style="color:gray;">Select food</p></option>
<option value="0">Kebab</option>
<option value="1">Mousaka</option>
<option value="2">Noodles</option>
</select>
<div id="log_in_container">
<div id="log_in">Log_In_button</div>
<div id ="log_in_form">login_form</div>
</div>
<div id="datepicker"></div>
</body>
</html>
In the above snippet if you click at Log_in_button the Log_in_form appears. If then you click on a date it doesn't disappear, then click on a select the Log_in_form still doesn't disappear, after that click on the next select the Log_in_form is still visible. I would like to make it so that it disappears in these occasions (like select pop up does). Is this possible?