0

I tried searching for a way to hook the mouseleave event on any document and could accomplish this snippet:

let hook = $(this).mouseleave(() => {
    alert('mouseleave')
});
$(this).mouseleave = (...args) => (alert('mouseleave'));

It successfully registers any mouseleave events, but my goal is to replace the existing mouseleave function of a page to my function of choice. The question is: Is there a way to capture the existing mouseleave events globally to replace them with my own function?

Martin
  • 16,093
  • 1
  • 29
  • 48
  • Are you looking to *remove* the existing events and then add your own, or add your own as a wrapper? (ie your event calls the original event) – freedomn-m Nov 14 '19 at 08:29
  • Is it a third-party page or your own? If the existing event is fired in bubbling mode, you could add another listener in capture mode, and if the existing listener is not needed, stop the propagation in your handler. – Teemu Nov 14 '19 at 08:29
  • Looks like you're asking: how do I find existing events in order to replace them - short answer: you can't – freedomn-m Nov 14 '19 at 08:32
  • A third party page. I am looking to remove the existing events and add my own. – Nikita Zaliven Nov 14 '19 at 08:32
  • related question, *possible* duplicate: https://stackoverflow.com/a/18116524/2181514 – freedomn-m Nov 14 '19 at 08:34

1 Answers1

0

replace the existing mouseleave

existing mouseleave events globally

you can remove any event from a node using .off(), eg

$("*").off("mouseleave");

to remove all mouseleave events from all elements

More info: https://api.jquery.com/off/

Community
  • 1
  • 1
freedomn-m
  • 27,664
  • 8
  • 35
  • 57