I'm trying to make panel that opens when it's clicked on the button. I have the button, I have the panel. With click()
event it does open. When that button is pressed again, it does close.
$('#button').click(function() {
$('#panel').toggle();
});
I want to achieve that if user clicks everywhere except on #button
or #panel
, it does close too.
P.S. I tried something like this, but it's not the wanted behavior.
$('#button').mouseenter(function() {
$('#panel').show();
}).mouseleave(function() {
setTimeout(function() {
$('#panel').hide();
}, 2000);
});
$(
function(){
$("#button").click( function(){ $("#panel").toggle(); } );
$(document).click( function(e){
var elm = jQuery(e.target);
if(elm.is("#button") || elm.is("#panel") || elm.parents("#panel").length>0)return;
$("#panel").hide();
});
}
);
Example
Checks to make sure that the element that was clicked [e.target
] is not
elm.is("#button")
elm.is("#panel")
elm.parents("#panel").length>0
Try this
$('#button').click(function(e) {
$('#panel').toggle();
e.stopPropagation();
});
$('#panel').click(function(e) {
e.stopPropagation();
});
$(document.body).click(function(e) {
if($('#panel').is(":visible")){
$('#panel').hide();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With