Attaching Events

As we discussed briefly in the section Bunch SET methods, there are methods available to help you easily and quickly fetch a node, or series of nodes, and attach event handlers to the bunch object that is returned. These methods are the query method, q(), to fetch the nodes, and the event attach method, on(), to insert the events:

//get node through the query method
var bunch = dom.q("QUERY");

//attach event onto the returned bunch object
bunch.on("EVENT", function(e){ ... }

In this example, we can see how to attach a click handler to a submit button to pop up an alert message:

var objSubmit = dom.q("#btnSubmit");
objSubmit.on("click", function(e){
   alert("You have just clicked the button - you win a prize");
});

The click event is just one of many DOM events that are supported through the event attach method. If we explore the full list of available events shown in Table 8-12, we see that there are several user-initiated features that we can account for in our programs.

Table 8-12. DOM events supported through the event attach method

Event

Description

blur

The focus on the element has been lost.

change

The user has changed the value of an input, select, or textarea field.

click

The user has clicked on the object with her mouse.

doubleclick

The user has clicked on the object twice with her mouse.

enterkey

The user has pressed the Enter key while in a focused element.

escapekey

The user has pressed the Escape key while in a focused element.

focus

The user has given an object focus.

keypress

The object currently in focus has received keyboard input from the user.

mousedown

The mouse button has been pressed down while the mouse is over the element.

mouseout

The mouse has moved off the element.

mouseover

The mouse has moved over the element.

mouseup

The mouse button has been released while the mouse is over the element.

specialclick

The user has clicked the alternative mouse button while hovering her mouse over the element.

There are two instances in which the event handlers for these events may be triggered:

  • The user has performed an action that has triggered the event.

  • The developer has programmatically triggered the event without user interaction.

If the developer triggers the events without user interaction, the browser’s default action will not be triggered. For the default browser action to be triggered when the event is triggered, the user must be the event initiator.

When the event is triggered, the function that is associated with the event will be triggered:

objSubmit.on("click", function(e){ ... }

The function that is triggered will contain one argument, the event object. This event object can contain a number of properties that allow you to determine information about the event programmatically once it has been triggered. This object will help you to modularize your event handler so that you have a single handler for all events that uses the information from the event object to determine what action it should take.

Table 8-13 lists all of the properties that may be set within an event object.

Table 8-13. Event object properties

Property

Description

altKey

The state of the Alt key (pressed or not), represented as a Boolean.

ctrlKey

The state of the Control key (pressed or not), represented as a Boolean.

key

If a key was pressed to trigger the event, key will contain the code of the key pressed.

shiftKey

The state of the Shift key (pressed or not), represented as a Boolean.

target

A bunch that contains the target of the event.

that

A bunch that contains the node that is handling the event. This value may be the same as the target, or it may be a parent if bubbling occurs.

type

The event type displayed as a string.

x

The horizontal position of the mouse cursor relative to the target container.

y

The vertical position of the mouse cursor relative to the target container.

As you can see, there are many event features available for us to work with. This extensive list will help us to build highly targeted applications and sites that allow specific handling for user events and actions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.139.168