Events are fired or triggered in response to some action in the browser. Typical events are.
- Click Events
- Page Load Events
- Form Events
- Mouse Over Events.
Take a look at these Events.
-
Type
What kind of event it is. For example, mousemove, keydown, click, doubleclick are all event types. -
Target
Object where the event happened. For example, an element, a window can be event targets. -
Handler
Function that gets invoked when an Event is fired.
Each Event has an event object that has a set of properties associated with it. This object is created when the Event is fired. Some properties are the event type and target.
For most browsers, not IE, use the addEventListener function to create a event handler.
document.getElementById('some-id').addEventListener(, , );
We are not going to go over IE functions for event handling. We'll use a library, like jQuery, that will provide event handler functions for IE and standards compliant browsers like Chrome, Firefox and Safari.
- simple_button.html
- simple_button2.html
- simple_button3.html (Level 2 DOM Events)
Fork, clone and complete this. Lab repo
How the events travel through the DOM.
-
The capture phase: The event object must propagate through the target's ancestors from the defaultView to the target's parent. This phase is also known as the capturing phase. Event listeners registered for this phase must handle the event before it reaches its target.
-
The target phase: The event object must arrive at the event object's event target. This phase is also known as the at-target phase. Event listeners registered for this phase must handle the event once it has reached its target. If the event type indicates that the event must not bubble, the event object must halt after completion of this phase.
-
The bubble phase: The event object propagates through the target's ancestors in reverse order, starting with the target's parent and ending with the defaultView. This phase is also known as the bubbling phase. Event listeners registered for this phase must handle the event after it has reached its target.
The first phase of propagation is capturing. Each ancestor of the element that is the target of the event may intercept the event and either ignore the event of do some processing.
IE does NOT implement Event Capturing, it only implements Event Bubbling
Each event handler may stop capturing by calling event.stopPropagation();
The last phase of propagation is bubbling. The event can bubble up the DOM tree. Each ancestor of the element generating the event will recieve the event after the target element This event is bubbling up the DOM tree.
Each event handler may stop capturing by calling event.stopPropagation();
Sometimes you will want to set handlers for DOM elements that will be inserted in the future. You can leverage event bubbling to handle this case. An ancestor DOM element may hold a handler for all, of some subset, of it's children. The way to do this is to :
- Add an event handler to an ancestor of this future element.
- In this ancestor event handler check the event.target, this is the source of event.
- If the event.target is the correct element then respond, do something.
- You can check the event.target id or class in this handler to determine how to respond to events fired on specific child elements.
This is a general techinque that is used when dynamically, at runtime, inserting nodes into the DOM. __ Ajax requests may insert DOM elements__.
If you have an element, like a list, with many children. It's not efficient to set handlers on each child element. In this case set the handler once on an ancestor element.
- Show how events can be captured and bubble up to ancestor elements. We'll create a parent div around the button. Then set a handler on this parent div. We'll show the event's target and current target. And what these two properties change. simple_button4.html
- dynamic_list.html
Fork, clone and complete this. Lab.
The problem is that the 'this' pointer in an event handler points to the element that fired the event.
If we want the 'this' pointer in the event handler to point to the same object that defined the handler we can use the javascript bind function.
- this_pointer.html
- this_callback.html