| Start |
|---|
| Delegated Events Let's start with example. Here is a very simple example HTML. Example HTMLThe problemNow in this example, we want to add an event listener to all The problem we have is how to add events to the Background information - Event propagationDelegated events are only possible because of event propagation (often called event bubbling). Any time an event is fired, it will bubble all the way up (to the document root). They delegate the handling of an event to a non-changing ancestor element, hence the name "delegated" events. So in example above, clicking
SolutionKnowing what event bubbling does, we can catch one of the wanted events which are propagating up through our HTML. A good place for catching it in this example is the In above:
In detail how solution works
Note: If a suitable non-changing ancestor is not available/convenient, you should use
|
Attach and Detach Event Handlers Attach an Event HandlerSince version 1.7 (opens new window) jQuery has the event API HTMLjQueryDetach an Event HandlerNaturally you have the possibility to detach events from your jQuery objects too. You do so by using HTMLjQueryWhen clicking the button jQueryIn this case the |
| Document Loading Event .load() If you want your script to wait until a certain resource was loaded, such as an image or a PDF you can use HTML jQuery |
| Events for repeating elements without using ID's Problem There is a series of repeating elements in page that you need to know which one an event occurred on to do something with that specific instance. Solution
HTML jQuery |
| originalEvent Sometimes there will be properties that aren't available in jQuery event. To access the underlying properties use Get Scroll Direction |
| Switching specific events on and off via jQuery. (Named Listeners) Sometimes you want to switch off all previously registered listeners. An issue with this method is that ALL listeners binded on More often than not, we want to detach all listeners attached only by us. To achieve this, we can bind named listeners as, This ensures that any other click listener is not inadvertently modified. RemarksjQuery internally handles events via the addEventListener (opens new window) function. This means it is perfectly legal to have more than one function bound to the same event for the same DOM element. |
Read Article
https://develop.horje.com/learn/1434/reference