JQuery / Javascript temporarily disables addEventListener / attachEvent related events

Is there a way to temporarily disable the event listener?

In my case, I have a third-party library ( not jQuery centric) that fires mouseover / mouseout events for an element using addEventListener / attachEvent .

Under certain circumstances, another event fires on a different element, and I need to disable these event listeners. My solution so far has been just unbindmouseover / mouseout. This is usually normal, because this event usually causes a page refresh.

However, an error occurs from time to time (I think a validation error), which causes the page to not refresh, and I need to re-attach mouse / mouse event listeners.

Helpful information

It might be worth mentioning that since mouseover / mouseout event listeners are created and attached in a third-party library, I cannot just assign the event to a variable and bind / untie it in such a way (that AFIK is the best way to do this).

Update

I originally asked

Is there a way in jQuery to get event listeners already assigned to an object?

Since then, I found out that it is not possible to access events assigned by addEventListener / attachEvent: Access events added using attachEvent () / addEventListener () in JavaScript

+3
source
3

jQuery data , :

$("#foo").data("events")

, unbind:

$("#foo").unbind('click', $("#foo").data("events").click[42]);
+1

, . W3C removeEventListener (docs) / Microsoft detachEvent (docs). , , .

removeEventListener, , , , , ; .

, . W3C.

+1

If you want to temporarily disable the event handler, why not just add the escape code to the function?

So:

$('#button').click(function(){
    var clicked_element = $(this);
    if(elem.hasClass('event-click-disabled'))
    {
        // logging code so we know exactly what events are being skipped
        console.info(
            'The click event on following element was skipped',
            clicked_element
        );
        return;
    }
    alert('Button clicked');
});

Then, if you want to disable the event for a specific item, just call

element.addClass('event-click-disabled');

The event handler is still running, but it will return immediately.

0
source

Source: https://habr.com/ru/post/1795638/


All Articles