Triggering Specific Event Handlers - J Query

Problem
You need to trigger an event on a certain element (or many). This element belongs to one or more plugins so it may have event handlers bound to this event.
The problem is that this event is a common one, like click or mousedown. Simply triggering the event could run other event handlers that you didn’t expect.

Solution

On the same principle as the previous recipe, namespaces can be used for triggering as well. When binding, you need to make sure you add a unique namespace to each set of handlers.
This can also be used for the opposite situation; if you need to trigger any event except those with a namespace, you can use the ! operator. An example of this will be shown in the discussion.

Discussion

How to trigger handlers with a certain namespace?

Now, say you want to programmatically trigger the click event bound by the plugin myPlugin. You could simply trigger the click event, but that would be a bad approach,because any other handler bound to the same event would get fired as well.
This is how to do this properly:

How to trigger handlers that do not have a namespace?

On the contrary, maybe you need to trigger a click (or any other event), but the target element belongs to one or more plugins. Triggering an event could run undesired event handlers, and that would cause problems that will be pretty hard to debug.
So, assuming all the plugins did use a namespace, this is how to trigger a click safely:


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics