hellomello
hellomello

Reputation: 8597

stop mouseleave when clicked on an object

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();
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class='div'>
       <div class='element' style='display:none;'>
          boxed element
       </div>
       <div class='dropdown' style='display:none;'>
          menu
       </div>
    </div>

Upvotes: 7

Views: 10147

Answers (5)

Tim de Vos
Tim de Vos

Reputation: 1

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.
    }
});

Upvotes: 0

Mex
Mex

Reputation: 91

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>

Upvotes: 0

Dally S
Dally S

Reputation: 703

This did the trick for me

$("#id").click(function(){
     $("#id").off("mouseleave");
});

Upvotes: 7

techfoobar
techfoobar

Reputation: 66693

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();
    }

});

Upvotes: 4

adeneo
adeneo

Reputation: 318342

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.

Upvotes: 0

Related Questions