I'm trying to make a Div that is initially hidden to show up and stay toggled unless i do one of the following events (so in that case they hide again):
- reclick on the same element that displayed the div on the first hand
- click outside the div (container)
- click outside of a div's children (descendant of the container)
This is the code i come up with : (#menu_login being the menu on which i click to make the div appear and .bubble being the div in question)
$(document).ready(function(){
$(".bubble").hide();
$("#menu_login").click(function(){
$(".bubble").toggle();
});
$(document).mouseup(function (e)
{
var container = $(".bubble");
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();
}
});
So first, the hide_on_load function works well, but the toggle and mouseup function seems to be in a conflict when running together.
They work well when i use only one of them (and i really like this mouseup function because of the way it allows me to click on the container's descendants even when outside of the div).
The thing happening is when i click on the menu and toggle the div, if i click it again to untoggle and hide the div my mouseup function creates a conflict : It looks like because i on the menu again to untoggle and hide the div again, the mouseup function also detects that i clicked outside of my div and hide my div before my toggle function can. So what happen is that the toggle function won't detect that i clicked the menu to untoggle and hide the div, but that i clicked to toggle and show the div because the mouseup function closed it 0.01sec before.
So i don't really know how to do to have theses two functions working together, i had some kind of idea about adding an exeption that says to the mouseup function not to hide the div if i click on the menu (the same it doesnt close it if i click on a div's descendant) so my toggle function can do it and there is no conflict, but my knowledges in JQuery are non-existent.
exemple :
$(document).mouseup(function (e)
{
var container = $(".bubble");
if (!container.is(e.target) container...
&& container.has(e.target).length === 0)
// and here : "don't close it if i click on my #menu_login neither
{
container.hide();
}
Any help/suggestion is welcome, sorry if this a bit heavy to read but i tried to be as specific as possible.
Thank you !
-Apatik