0

I have something like the following in my js code...

    a.off('mouseover').on('mouseover', function() {
        overlayTrigger(this);
    });
    a.off('click').on('click', function() {
        overlayTrigger(this, true);
    });

And then later on during the mouse click, i remove them:

    $(obj).off('mouseover');
    $(obj).off('mouseout');

But I want the next mouse click, if those handles are off, I want to re-enable them... However, I don't know how to check whether 'mouseover' is off or not...

Digging through google is a little bit difficule, because the keywords "on" and "off" and "mouseover" aren't the method to find what i want... >.<

Any help/hint would be appreciated. :)

codenamezero
  • 2,724
  • 29
  • 64
  • 1
    Well...if the click handler is `off`, then you can't really have any logic on the next click, since the handler is...off? I also don't quite understand why you would do this: `a.off('mouseover').on('mouseover', function() {` – tymeJV Jun 20 '13 at 21:11
  • Answered here: http://stackoverflow.com/questions/1515069/jquery-check-if-event-exists-on-element – Tushar Roy Jun 20 '13 at 21:13
  • That is when i try to bind it, is in different part of the code, anyhow, i read it somewhere here that is to prevent binding the event multiple time to the object. – codenamezero Jun 21 '13 at 13:14

2 Answers2

1

You can get a list of events bound to an object using jQuery.data.

If you are using jQuery 1.8 or above you will need to use the following:

jQuery post here.

$._data(elem, 'events');

elem should be an HTMLElement, NOT a jQuery object or selector. If you have a jQuery object already you can use replace elem with elem.get(0).

jQuery version below 1.8:

$(elem).data('events');

This will return an object with the events. You can then check to see if they are undefined or not.

NOTE: Keep in mind that if there are no event handlers on the element, the data will return as undefined.

technicallyjosh
  • 3,511
  • 15
  • 17
  • Wow, thank you, how did one even find this to begin with!!! This is perfect. I was monitoring `$._data(obj, 'events');` and could clearly see the event being remove one by one as it go. Much appreciated. :) – codenamezero Jun 21 '13 at 13:04
  • Haha you are welcome. Sometimes when there isn't clear documentation, digging in and looking at the code helps. :) Glad I could help! – technicallyjosh Jun 21 '13 at 17:52
0

while re-enabling them , you could do this

$(obj).off('mouseover').on('mouseover',function(){...});
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111