jqueryevent-handlingjquery-eventsjquery-on

How to detect jQuery event (ex. click mouseout) on or off?


For default

$().mouseout(function(){…})

The mouseout event is on in the page

Can I use any way to detect the event is whether on?

I want the detect method return a Boolean for me to use.

Hope to see your answer.


Solution

  • Using $._data, yea!

    Yes, you can do so by checking this way:

    console.log($._data( $(element)[0], "events" ));
    

    Example

    ​$("#foo").on({
        click: function(){ alert("Hello") },
        mouseout: function(){ alert("World") }
    });​​​
    ​$._data( $("#foo")[0], "events" );
    

    Output

    Output

    In your code:

    So, you can use the returning object and perform operations.

    myEvents = ​$._data( $("#foo")[0], "events" );
    // Check if the mouse event is set
    if (myEvents.mouseout.length > 1)
        // Do something