-2

Existing code already uses .on to bind all over the place. I need to insert html into the page, but I have my own logic for initializing the html and binding the clicks and therefore for the piece I am about to insert, I would not like the live features of jQuery. but run it through my own binders.

Is there a way to do that? To turn off the autobinding for .on temporarily ?

mjs
  • 21,431
  • 31
  • 118
  • 200
  • Why is this question on hold? I think it's clear enough. LiveQuery automatically binds events on any new element that match a previously-created DOM Mutation Observer for that specific selector, he wants to bypass that without rewriting the current codebase. – Ravenous Mar 08 '18 at 19:55

1 Answers1

1

Yes, jQuery-natively, there is off which can unbind existing events, either all or particular (namespaced) and there is event.stopImmediatePropagation() that allows you to stop event bubbling and tells the program not to execute any additional event handlers. (you need to bind before the other handlers in order to prevent them from executing, first to come is the first to be served).

Particularly to LiveQuery, there is expire. Read doc at https://github.com/hazzik/livequery/blob/master/README.md

But you can always affect what a plugin does with native techniques if you are hackish enough. I had to do a lot of things like that on sites created by others.

Ravenous
  • 1,112
  • 11
  • 25
  • Thank you. The off method, you mentioned a namespace. I do not control the existing binding, so I am unsure if that is possible. I'd like to insert html just once without triggering jquery on. Is there a way to turn it off globally, and then you could turn it on right after insertion? What a mess. Do you btw know if appending the html without jquery (innerhtml) still triggers on ? – mjs Mar 08 '18 at 12:24
  • You don't need to control the existing binding, `off` is useful in terms of namespaced events cause you can cancel particular events and keep the ones you don't want to unbind, `$(el).off('click')` for example, will destroy ALL the event listeners on click. – Ravenous Mar 08 '18 at 13:57
  • 1
    And I've just read how LiveQuery works, it uses DOM Mutation Observers, that means yes, it will still trigger and will still rebind the new elements. Just use the expire method of https://github.com/brandonaaron/livequery and do your stuff after getting rid of LiveQuery on your particular selectors – Ravenous Mar 08 '18 at 14:00