If I want to mouseenter a div, an element will show, then when mouseleave, element disappears.
I have a click function inside the mouseenter so theres a drop down menu when clicked.
I want the dropdown menu and the element to stay active even when mouseleaves. So I want mouseleaves to not be applicable in this situation, when there's a click event. User will have to click on the element again or anywhere on the page to make the element and dropdownmenu disappear.
But I want to keep mouseleave function working so if user mouseenters a div, the element will show and they can click on it again.
I currently have something like this, but I just dont know how to make the mouseleave to not be applicable when clicked
$(".div").mouseenter(function(){
$(".element",this).show();
$(".element").click(function(){
$(".dropdown").show();
return false;
});
}).mouseleave(function(){
$(".element",this).hide();
});
EDIT:
HTML
<div class='div'>
<div class='element' style='display:none;'>
boxed element
</div>
<div class='dropdown' style='display:none;'>
menu
</div>
</div>
You can set a custom attribute for marking the item as clicked. The mouseleave event handler can check the value of this custom attribute before hiding the element.
i.e. Something like:
$(".div").mouseenter(function(){
$(".element",this).show();
$(".element").click(function(){
$(".dropdown").show();
// set custom attribute for marking this one as clicked
$(this).attr('clicked', 'yes');
return false;
});
}).mouseleave(function(){
// check custom attribute before hiding the element
if($(this).attr('clicked') != 'yes') {
$(".element",this).hide();
}
});
This did the trick for me
$("#id").click(function(){
$("#id").off("mouseleave");
});
You can use jQuery's new on/off methods for this if your using 1.7+
Something like:
$(".div").on('mouseenter', function(){
showElm();
$(".element").click(function(){
$(".div").off('mouseleave', hideElm);
$(".dropdown").show();
return false;
});
});
$(".div").on('mouseleave', hideElm);
$(document).on('click', hideElm);
function hideElm() { $(".element, .dropdown").hide(); }
function showElm() { $(".element").show(); }
Fiddle: http://jsfiddle.net/fSjtm/
Will probably need some tweeking, but it will give you a general idea.
use event.stopPropagation();
this is better because you can use links on element
$(".div").on('mouseenter', function(){
showElm();
$(".element").click(function(event){
$(".div").off('mouseleave', hideElm);
$(".dropdown").show();
event.stopPropagation();
});
});
$(".div").on('mouseleave', hideElm);
$(document).on('click', hideElm);
function hideElm(event) { $(".element, .dropdown").hide(); }
function showElm(event) { $(".element").show(); }
.element {position: absolute; top: 20px; height: 100px; width: 100px; display:none; background: red; }
.dropdown {position: absolute; top: 120px; height: 400px; width: 100px; background: blue; display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='div'>HERE
<div class='element'>
boxed element
</div>
<div class='dropdown'>
menu
</div>
</div>
It seems the mouseleave event you refer to, is custom triggered by the jquery click and not an original mouseEvent. Try this:
$(".div").on('mouseenter', function(){
$(".element").on('click', function(){ ... });
});
$(".div").on('mouseleave', function(event){
if(typeof(e.originalEvent) != 'undefined'){
// only when mouseleave is the original event.
}
});