I have a div
that I hide/show with onclick
that adds or removes a css class
. I want to be able to close/hide the div when the user clicks outside the div
area.
How do I do this?
I have a div
that I hide/show with onclick
that adds or removes a css class
. I want to be able to close/hide the div when the user clicks outside the div
area.
How do I do this?
I think what you're looking for can be done with JavaScript. Its an event called 'onBlur' which triggers an event handler when something loses focus (e.g. your div).
You can read more about it here: http://www.w3schools.com/jsref/event_onblur.asp
Basically you call a JavaScript function when your object loses focus:
onBlur="myFunction();"
...and 'myFunction()' will have the JavaScript to change your CSS.
As mentioned here- https://stackoverflow.com/a/7385673/1982631
HTML
<div class="block">
<div class="blockelements">
</div>
</div>
jQuery-
$(document).bind('click mouseup blur',function (e)
{
var container = $(".block");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});