Working with Events
In this chapter, I describe the jQuery support for events. If you are unfamiliar with events, then I provided a brief overview of how they work and how they are propagated through the DOM (Domain Object Model) in Chapter 2. jQuery provides some useful event-related features, of which my favorite is the ability to automatically associate event handler functions with elements as they are added to the DOM. Table 9-1 provides the summary for this chapter.
Table 9-1. Chapter Summary
Problem | Solution | Listing |
---|---|---|
Register a function to handle one or more event | Use the bind method or one of the shorthand methods | 1–4, 19, 20, 23 |
Suppress the default action for an event | Use the Event.preventDefault method or use the bind method without specifying a handler function | 5–6 |
Remove an event handler function from an element | Use the unbind method | 7–9 |
Create a handler function that is executed only once for each element it is associated with | Use the one method | 10 |
Automatically apply an event handler function to elements as they are added to the document | Use the on method | 11–13 |
Remove a handler created using the live method | Use the off method | 14 |
Apply an automatically added handler to a specific element in the DOM | Use the delegate and undelegate methods | 15 |
Manually invoke the event handler functions for an element | Use the trigger or triggerHandler method or one of the shorthand methods | 16–18, 21, 22 |
JQUERY CHANGES SINCE THE LAST EDITION
jQuery 1.9/2.0 has removed the live and die methods. The same functionality is available through the on and off methods, which are described in the section “Performing Live Event Binding.”
Some behind-the-scenes changes have been made to the order in which focus and blur events are sent when the trigger method is used so they better follow the sequence that would be seen had the events been triggered by the user. See the section “Manually Invoking Event Handlers” for details of how to use the trigger method.
A related change is that when you trigger the click event on a checkbox or radio button input element, the event handlers will receive the new state of the element, which is consistent with the effect caused by the user changing the state of the element. In previous versions of jQuery, the handlers would receive the old state.
I omitted some jQuery features in the last edition of this book because they were marked as deprecated. The resulting changes in the latest versions of jQuery don’t affect the content in this chapter but are worth mentioning in case you have found references to them on jquery.com and started using them in your projects: the attrChange, attrName, relatedNode, and srcElement properties have been removed from the jQuery Event object; the hover pseudo-event is no longer supported (but the hover method I describe in the section “Using the Event Shorthand Methods” is unaffected); the toggle method, which alternated between two event handler functions for the same event, has been removed.
Handling Events
jQuery provides a set of methods that let you register functions that are called when specified events are triggered on elements you are interested in. Table 9-2 describes these methods.
Table 9-2. Methods for Handling Events
Method | Description |
---|---|
bind(eventType, function)bind(eventType, data, function) | Adds an event handler to the elements in a jQuery object with an optional data item |
bind(eventType, boolean) | Creates a default handler that always returns false, preventing the default action; the boolean argument controls event bubbling |
bind(map) | Adds a set of event handlers based on a map object to all elements in the jQuery object |
one(eventType, function)one(eventType, data, function) | Adds an event handler to each element in a jQuery object with an optional data item; the handler will be unregistered from an element once it has been executed. |
unbind() | Removes all event handlers on all elements in the jQuery object |
unbind(eventType) | Removes a previously registered event handler from all elements in the jQuery object |
unbind(eventType, boolean) | Removes a previously registered always-false handler from all elements in the jQuery object |
unbind(Event) | Removes an event handler using an Event object |
The various flavors of the bind method let you specify a function that will be invoked when an event is triggered, and since this is jQuery, the function is used for all of the elements in the jQuery object on which the bind method is called. Listing 9-1 shows a simple example.
Listing 9-1. Using the bind Method to Register an Event Handler Function
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script src="jquery-2.0.2.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<script type="text/javascript">
$(document).ready(function() {
function handleMouseEnter(e) {
$(this).css({
"border": "thick solid red",
"opacity": "0.5"
});
};
function handleMouseOut(e) {
$(this).css({
"border": "",
"opacity": ""
});
}
$("img").bind("mouseenter", handleMouseEnter)
.bind("mouseout", handleMouseOut);
});
</script>
</head>
<body>
<h1>Jacqui's Flower Shop</h1>
<form method="post">
<div id="oblock">
<div class="dtable">
<div id="row1" class="drow">
<div class="dcell">
<img src="aster.png"/><label for="aster">Aster:</label>
<input name="aster" value="0" required />
</div>
<div class="dcell">
<img src="daffodil.png"/><label for="daffodil">Daffodil:</label>
<input name="daffodil" value="0" required />
</div>
<div class="dcell">
<img src="rose.png"/><label for="rose">Rose:</label>
<input name="rose" value="0" required />
</div>
</div>
<div id="row2"class="drow">
<div class="dcell">
<img src="peony.png"/><label for="peony">Peony:</label>
<input name="peony" value="0" required />
</div>
<div class="dcell">
<img src="primula.png"/><label for="primula">Primula:</label>
<input name="primula" value="0" required />
</div>
<div class="dcell">
<img src="snowdrop.png"/><label for="snowdrop">Snowdrop:</label>
<input name="snowdrop" value="0" required />
</div>
</div>
</div>
</div>
<div id="buttonDiv"><button type="submit">Place Order</button></div>
</form>
</body>
</html>
In Listing 9-1, I select all of the img elements in the document and use the bind method to register handler functions for the mouseenter and mouseout events. These handlers use the css method to set values for the border and opacity properties. When the user moves the mouse pointer over one of the img elements, the border is drawn and the image is made more transparent, returning to its previous state when the pointer is moved away.
When jQuery calls the handler function, the this variable is set to the element to which the handler is attached. The object passed to the handler function is jQuery’s own Event object, which is different from the Event object defined by the DOM specification. Table 9-3 describes the properties and methods of the jQuery Event object.
Table 9-3. Members of the jQuery Event Object
Name | Description | Returns |
---|---|---|
currentTarget | Gets the element whose listeners are currently being invoked | HTMLElement |
Data | Gets the optional data passed to the bind method when the handler was registered; see the section “Registering a Function to Handle Multiple Event Types” for details | Object |
isDefaultPrevented() | Returns true if the preventDefault method has been called | Boolean |
isImmediatePropagationStopped() | Returns true if the stopImmediatePropagation method has been called | Boolean |
isPropagationStopped() | Returns true if the stopPropagation method has been called | Boolean |
originalEvent | Returns the original DOM Event object | Event |
pageXpageY | Returns the mouse position relative to the left edge of the document | number |
preventDefault() | Prevents the default action associated with the event from being performed | void |
relatedTarget | For mouse events, returns the related element; this varies depending on which event has been triggered | HTMLElement |
Result | Returns the result from the last event handler that processed this event | Object |
stopImmediatePropagation() | Prevents any other event handlers being called for this event | void |
stopPropagation() | Prevents the event from bubbling but allows handlers attached to the current target element to receive the event | void |
Target | Gets the element that triggered the event | HTMLElement |
timeStamp | Gets the time at which the event was triggered | number |
Type | Gets the type of the event | string |
Which | Returns the button or key that was pressed for mouse and keyboard events | number |
The jQuery Event object also defines most of the properties from the standard DOM Event object. So, for almost all situations, you can treat the jQuery Event object as having a superset of the functionality defined by the DOM standard.
Registering a Function to Handle Multiple Event Types
A common technique is to use a single function to handle two or more kinds of event. These events are usually related in some way, such as the mouseenter and mouseout events. When using the bind method, you can specify multiple event types in the first argument, separated by a space. Listing 9-2 demonstrates this method.
Listing 9-2. Registering a Function to Handle Multiple Event Types
...
<script type="text/javascript">
$(document).ready(function() {
function handleMouse(e) {
var cssData = {
"border": "thick solid red",
"opacity": "0.5"
}
if (event.type == "mouseout") {
cssData.border = "";
cssData.opacity = "";
}
$(this).css(cssData);
}
$("img").bind("mouseenter mouseout", handleMouse);
});
</script>
...
In this script, I have used a single call to the bind method to specify that the mouseenter and mouseout events should be handled by the handleMouse function for all of the img elements in the document. Of course, you can also use a single function and chain the bind calls, as follows:
...
$("img").bind("mouseenter", handleMouse).bind("mouseout", handleMouse);
...
You can also register handlers using a map object. The properties of the object are the names of the events, and their values are the functions that will be invoked when the events are triggered. Listing 9-3 shows the use of a map object with the bind method.
Listing 9-3. Using a Map Object to Register Event Handlers
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind({
mouseenter: function() {
$(this).css("border", "thick solid red");
},
mouseout: function() {
$(this).css("border", "");
}
});
});
</script>
...
In Listing 9-3, I have defined the handler functions inline, as part of the map object. The bind method uses the functions I specify as handlers for the events that correspond to the property names in the map object.
Providing Data to the Event Handler Function
You can pass an object to the bind method, which jQuery will then make available to the handler function through the Event.data property. This can be useful when using a single function to handle events from different sets of elements. The data value can help determine what kind of response is required. Listing 9-4 shows how to define and use the data value.
Listing 9-4. Passing Data to the Event Handler Function via the bind Method
...
<script type="text/javascript">
$(document).ready(function() {
function handleMouse(e) {
var cssData = {
"border": "thick solid " + e.data,
}
if (event.type == "mouseout") {
cssData.border = "";
}
$(this).css(cssData);
}
$("img:odd").bind("mouseenter mouseout", "red", handleMouse);
$("img:even").bind("mouseenter mouseout", "blue", handleMouse);
});
</script>
...
I use the optional argument to the bind method to specify which color border should be displayed when the mouseenter event is triggered. For the odd-numbered img elements, the border will be red and for the even-numbered it will be blue. In the event handler function I use the Event.data property to read the data and use it to create the value for the CSS (Cascading Style Sheet) border property. You can see the effect in Figure 9-1.
Figure 9-1. Passing data to the handler function via the bind method
Suppressing the Default Action
As I mentioned in Chapter 2, some events have a default action when they are triggered on certain elements. A good example occurs when the user clicks a button whose type attribute is submit. If the button is contained in a form element, the default action is for the browser to submit the form. To prevent the default action from being performed, you can call the preventDefault method on the Event object, as shown in Listing 9-5.
Listing 9-5. Preventing the Default Action on an Event
...
<script type="text/javascript">
$(document).ready(function() {
$("button:submit").bind("click", function(e) {
e.preventDefault();
});
});
</script>
...
This script sets up a handler function for the click event on all button elements whose type attribute is set to submit. The function only contains a call to the preventDefault method, which means that clicking the buttons won’t do anything at all, since the default action is disabled and the handler function doesn’t set up any alternatives.
Usually you want to suppress the default action so you can perform some other activity instead—for example, to stop the browser from submitting the form because you want to do it with Ajax (which is the topic of Chapters 14 and 15). Instead of writing a one-line function as I did in Listing 9-5, you can use a different version of the bind method, as shown in Listing 9-6.
Listing 9-6. Using the bind Method to Create a Handler That Prevents the Default Action
...
<script type="text/javascript">
$(document).ready(function() {
$("button:submit").bind("click", false);
});
</script>
...
The first argument is the event or events whose default action you want to suppress, and the second argument allows you to specify whether the event should be prevented from bubbling up the DOM (I explain event bubbling in Chapter 2).
Removing Event Handler Functions
The unbind method removes a handler function from an element. You can unbind all of the handlers associated with all events for all elements in a jQuery object by calling the unbind method with no arguments, as shown in Listing 9-7.
Listing 9-7. Unbinding All Event Handlers
...
<script type="text/javascript">
$(document).ready(function() {
function handleMouse(e) {
var cssData = {
"border": "thick solid red",
"opacity": "0.5"
}
if (event.type == "mouseout") {
cssData.border = "";
cssData.opacity = "";
}
$(this).css(cssData);
}
$("img").bind("mouseenter mouseout", handleMouse);
$("img[src*=rose]").unbind();
});
</script>
...
In Listing 9-7, I bind a handler for the mouseenter and mouseout events for all of the img elements and then use the unbind method to remove all of the handlers for the img element whose src attribute contains rose. You can be more selective by passing the events you want to unbind as an argument to the unbind method, as shown in Listing 9-8.
Listing 9-8. Selectively Unbinding Events
...
<script type="text/javascript">
$(document).ready(function() {
function handleMouse(e) {
var cssData = {
"border": "thick solid red",
"opacity": "0.5"
}
if (event.type == "mouseout") {
cssData.border = "";
cssData.opacity = "";
}
$(this).css(cssData);
}
$("img").bind("mouseenter mouseout", handleMouse);
$("img[src*=rose]").unbind("mouseout");
});
</script>
...
In this script I unbind only the mouseout event, leaving the handler for the mouseenter event untouched.
Unbinding from Within the Event Handler Function
The final option for unbinding is to do so from within the event handler function. This can be useful if you want to handle an event a certain number of times, for example. Listing 9-9 contains a simple demonstration.
Listing 9-9. Unbinding from an Event Inside the Event Handler
...
<script type="text/javascript">
$(document).ready(function() {
var handledCount = 0;
function handleMouseEnter(e) {
$(this).css("border", "thick solid red");
}
function handleMouseExit(e) {
$(this).css("border", "");
handledCount ++;
if (handledCount == 2) {
$(this).unbind(e);
}
}
$("img").bind("mouseenter", handleMouseEnter).bind("mouseout", handleMouseExit)
});
</script>
...
In the handleMouseEvent function, I increment a counter each time that I handle the mouseout event. After I have handled the event twice, I pass the Event object to the unbind method to unregister the function as a handler. jQuery figures out the details it requires from the event object.
Executing a Handler Once
The one method lets you register an event handler that will be executed only once for an element and then removed. Listing 9-10 provides an example.
Listing 9-10. Using the one Method to Register a Single-Shot Event Handler Function
...
<script type="text/javascript">
$(document).ready(function() {
function handleMouseEnter(e) {
$(this).css("border", "thick solid red");
};
function handleMouseOut(e) {
$(this).css("border", "");
};
$("img").one("mouseenter", handleMouseEnter).one("mouseout", handleMouseOut);
});
</script>
...
I have used the one method to register handlers for the mouseenter and mouseout events. The handler functions will be called when the user moves the mouse in and out of one of the img elements, and then the function will be unbound (but just for that element; the others will still have the handlers until the mouse is moved over them).
One limitation of the bind method is that your event handler functions are not associated with any new element that you add to the DOM. Listing 9-11 contains an example.
Listing 9-11. Adding Elements After Setting Up the Event Handlers
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind({
mouseenter: function() {
$(this).css("border", "thick solid red");
},
mouseout: function() {
$(this).css("border", "");
}
});
$("#row1").append($("<div class='dcell'/>")
.append("<img src='lily.png'/>")
.append("<label for='lily'>Lily:</label>")
.append("<input name='lily' value='0' required />"));
});
</script>
...
In this script, I use the bind method to set up handlers for the mouseenter and mouseout events for all of the img elements. I then use the append methods to insert some new elements in the document, including another img element. This new img element didn’t exist when I used the bind method, and my handler functions are not associated with it. The result of this is that I have six img elements that display a border when the mouse hovers over them and one that doesn’t.
In an example as simple as the one in Listing 9-11, the easy answer is to call the bind method again, but it can be difficult to keep track of which handlers are required for different types of elements. Fortunately, jQuery makes this easy for you with a set of methods that automatically register event handlers when new elements that match a selector are added to the DOM. Table 9-4 describes these methods.
Table 9-4. Methods for Automatically Registering Event Handlers
Method | Description |
---|---|
on(events, selector, data, function)on (map, selector, data) | Defines handlers for events for elements that exist now or in the future |
off(events, selector, function)off(map, selector) | Removes event handlers created using the on method |
delegate(selector, eventType, function)delegate(selector, eventType, data, function)delegate(selector, map) | Adds an event handler to the elements that match the selector (now or in the future) attached to the elements in the jQuery object |
undelegate()undelegate(selector, eventType) | Removes event handlers created with the delegate method for the specified event types |
Listing 9-12 shows the previous example updated to use the on method . The changes are minor, but the effect is significant. Any elements that I add to the DOM that match the selector img will have the functions in the map object set up as handlers for the mouseenter and mouseout events.
Listing 9-12. Using the on Method
...
<script type="text/javascript">
$(document).ready(function () {
$(document).on({
mouseenter: function () {
$(this).css("border", "thick solid red");
},
mouseout: function () {
$(this).css("border", "");
}
}, "img");
$("#row1").append($("<div class='dcell'/>")
.append("<img src='lily.png'/>")
.append("<label for='lily'>Lily:</label>")
.append("<input name='lily' value='0' required />"));
});
</script>
...
Notice that I call the on method on a jQuery object that is created from document. This ensures that my event handlers are applied to any img element that is added anywhere in the DOM. I can narrow the focus by changing the initial selector—for example, if I only wanted my handler functions to be applied to img elements added to the row1 element, I could have used the following call instead:
...
$("#row1").on(...map..., "img");
...
Tip The on method doesn’t need to add the handler functions directly to the element. In fact, it creates an event handler on the document object and looks for events that were triggered by elements that match the selector. When it sees such an event, it triggers the event handler. However, for all practical purposes, it is just easier to imagine the on method diligently adding handles to new elements.
Multiple events can be specified when using the on method. The event names are separated by space characters, either in the property names of the map object or in the first argument when the map isn’t used, as illustrated by Listing 9-13.
Listing 9-13. Specifying Multiple Events Using the on Method
...
<script type="text/javascript">
$(document).ready(function () {
function handleMouse(event) {
if (event.type == "mouseenter") {
$(this).css("border", "thick solid red");
} else if (event.type == "mouseout") {
$(this).css("border", "");
}
}
$("#row1").on("mouseenter mouseout", "img", handleMouse);
$("#row1").append($("<div class='dcell'/>")
.append("<img src='lily.png'/>")
.append("<label for='lily'>Lily:</label>")
.append("<input name='lily' value='0' required />"));
});
</script>
...
In Listing 9-13 I have used the version of the on method that doesn’t rely on a map object, specifying that the handleMouse function should be used for the mouseenter and mouseout events emitted by all img elements that are descendants of the row1 element.
The complement to the on method is off, which is used to remove the event handlers from existing elements and prevent them being used to respond to events from newly created elements. Listing 9-14 shows the use of the off method .
Listing 9-14. Using the off Method
...
<script type="text/javascript">
$(document).ready(function () {
function handleMouse(event) {
if (event.type == "mouseenter") {
$(this).css("border", "thick solid red");
} else if (event.type == "mouseout") {
$(this).css("border", "");
}
}
$("#row1").on("mouseenter mouseout", "img", handleMouse);
$("#row1").off("mouseout", "img");
$("#row1").append($("<div class='dcell'/>")
.append("<img src='lily.png'/>")
.append("<label for='lily'>Lily:</label>")
.append("<input name='lily' value='0' required />"));
});
</script>
...
Caution It is important to use the same selector with the on and off methods; otherwise, the off method won’t undo the effect of on.
Limiting DOM Traversal for Live Event Handlers
One problem with the on method is that the events have to propagate all the way up to the document element before your handler functions are executed. You can take a more direct approach by using the delegate method , which allows you to specify where the event listener will be located in the document. Listing 9-15 provides an example.
Listing 9-15. Using the delegate Method
...
<script type="text/javascript">
$(document).ready(function() {
$("#row1").delegate("img", {
mouseenter: function() {
$(this).css("border", "thick solid red");
},
mouseout: function() {
$(this).css("border", "");
}
});
$("#row1").append($("<div class='dcell'/>")
.append("<img src='carnation.png'/>")
.append("<label for='carnation'>Carnation:</label>")
.append("<input name='carnation' value='0' required />"));
$("#row2").append($("<div class='dcell'/>")
.append("<img src='lily.png'/>")
.append("<label for='lily'>Lily:</label>")
.append("<input name='lily' value='0' required />"));
});
</script>
...
In Listing 9-15, I use the delegate method to add the listener to the element whose ID is #row1, and the selector I specified matches the img element. The effect of this is that my handler functions will be executed when a mouseenter or mouseout event that originated from an img element propagates to the row1 element. When I add another img element to row1, it is automatically covered by my call to the delegate method, which is not the case when I add elements to row2.
The main benefit of using the delegate method is speed, which can become an issue if you have a particularly large and complex document and a lot of event handlers. By pushing the point where the events are intercepted down into the documents, you reduce the distance that events have to travel in the DOM before they lead to the handler functions being invoked.
Tip To remove handlers added with the delegate method, you have to use undelegate. The off method works only with the on method.
Manually Invoking Event Handlers
You can manually invoke the event handling functions on elements using the methods described in Table 9-5.
Table 9-5. Methods for Manually Invoking Event Handlers
Method | Description |
---|---|
trigger(eventType) | Triggers the handler functions for the specified event types on all of the elements in a jQuery object |
trigger(event) | Triggers the handler functions for the specified event on all of the elements in a jQuery object |
triggerHandler(eventType) | Triggers the handler function on the first element in the jQuery object, without performing the default action or bubbling the event |
Listing 9-16 shows how you can trigger the event handlers manually.
Listing 9-16. Triggering Event Handlers Manually
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind({mouseenter: function() {
$(this).css("border", "thick solid red");
},
mouseout: function() {
$(this).css("border", "");
}
});
$("<button>Trigger</button>").appendTo("#buttonDiv").bind("click", function (e) {
$("#row1 img").trigger("mouseenter");
e.preventDefault();
});
});
</script>
...
In this script, I use the bind method to set up a pair of event handler functions on the img elements in the document. I then use the appendTo method to insert a button element into the document method and the bind method to register a handler function for the click event.
When the button is pressed, the event handler function selects the img elements that are descendants of row1 and uses the trigger method to invoke their handlers for the mouseenter button. The effect, which is shown in Figure 9-2, is as though the mouse were moved over all three img elements.
Figure 9-2. Manually triggering event handler functions
Using an Event Object
You can also use an Event object to trigger other elements’ event handlers. This can be a convenient technique to use inside a handler, as demonstrated in Listing 9-17.
Listing 9-17. Manually Triggering Event Handles with an Event Object
...
<script type="text/javascript">
$(document).ready(function() {
$("#row1 img").bind("mouseenter", function() {
$(this).css("border", "thick solid red");
});
$("#row2 img").bind("mouseenter", function(e) {
$(this).css("border", "thick solid blue");
$("#row1 img").trigger(e);
});
});
</script>
...
In Listing 9-17, I use the bind method to add a red border to the img descendants of the row1 element in response to the mouseenter event. I do the same with a blue border to the row2 img elements, but in the handler, I have added the following statement:
..
$("#row1 img").trigger(e);
...
The effect of this addition is that when the mouse enters one of the row2 img elements, the handler for the same event type is triggered on the row1 img elements as well. You can see the effect in Figure 9-3.
Figure 9-3. Triggering event handlers using an event
This approach is convenient when you want to trigger the handlers for the event type currently being processed, but you could as easily get the same effect by specifying the event type.
Using the triggerHandler Method
The triggerHandler method invokes the handler functions without performing the event’s default action or allowing the event to bubble up through the DOM. And, unlike the trigger method, triggerHandler invokes the handler function only on the first element in a jQuery object. Listing 9-18 shows the use of this method.
Listing 9-18. Using the triggerHandler Method
...
<script type="text/javascript">
$(document).ready(function() {
$("#row1 img").bind("mouseenter", function() {
$(this).css("border", "thick solid red");
});
$("#row2 img").bind("mouseenter", function(e) {
$(this).css("border", "thick solid blue");
$("#row1 img").triggerHandler("mouseenter");
});
});
</script>
...
Tip The result from the triggerHandler method is the result returned by the handler function, which means you cannot chain the triggerHandler method.
You can see the effect of this script in Figure 9-4.
Figure 9-4. Using the triggerHandler method
Using the Event Shorthand Methods
jQuery defines some convenience methods that you can use as shorthand to register an event handler for commonly used events. In the tables that follow, I have shown these shorthand methods with a function argument. This is the most common use and is equivalent to calling the bind method, but these methods require less typing and (at least to my mind) make it more obvious which events you are binding to. Listing 9-19 shows how you can use a shorthand method in this way.
Listing 9-19. Using an Event Shorthand Method to Bind a Handler Function
...
<script type="text/javascript">
$(document).ready(function() {
$("img").mouseenter(function() {
$(this).css("border", "thick solid red");
});
});
</script>
...
This is equivalent to using the bind event for the mouseenter event, which I have shown in Listing 9-20.
Listing 9-20. Using the bind Method for the mouseenter Event
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind("mouseenter", function() {
$(this).css("border", "thick solid red");
});
});
</script>
...
That’s all well and good, and by this point, you should be comfortable with how this example works. However, you can also use the shorthand methods as an analog to the trigger method. You do this by calling the method without arguments. Listing 9-21 shows how you can do this.
Listing 9-21. Using the Event Shorthand Methods to Trigger Event Handlers
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind("mouseenter", function() {
$(this).css("border", "thick solid red");
});
$("<button>Trigger</button>").appendTo("#buttonDiv").click(function (e) {
$("img").mouseenter();
e.preventDefault();
});
});
</script>
...
I add a button to the document that, when clicked, selects the img elements and invokes their handlers for the mouseenter event. For completeness, Listing 9-22 shows the equivalent functionality written using the trigger method.
Listing 9-22. Using the trigger Method
...
<script type="text/javascript">
$(document).ready(function() {
$("img").bind("mouseenter", function() {
$(this).css("border", "thick solid red");
});
$("<button>Trigger</button>").appendTo("#buttonDiv").click(function (e) {
$("img").trigger("mouseenter");
e.preventDefault();
});
});
</script>
...
In the sections that follow, I list the different categories of shorthand methods and the events they correspond to.
Using the Document Event Shorthand Methods
Table 9-6 describes the jQuery shorthand methods that apply to the document object.
Table 9-6. Document Event Shorthand Methods
Method | Description |
---|---|
load(function) | Corresponds to the load event, triggered when the elements and resources in the document have been loaded |
ready(function) | Triggered when the elements in the document have been processed and the DOM is ready to use |
unload(function) | Corresponds to the unload event, triggered when the user navigates away from the page |
The ready method deserves special mention. It doesn’t correspond directly to a DOM event but is incredibly useful when using jQuery. You can see the different ways you can use the ready method in Chapter 5, where I explain how to defer execution of a script until the DOM is ready and how you can control the execution of the ready event.
Using the Browser Event Shorthand Methods
Table 9-7 describes the browser events, which are usually targeted at the window object (although the error and scroll events are also used with elements as well).
Table 9-7. Browser Event Shorthand Methods
Method | Description |
---|---|
error(function) | Corresponds to the error event, triggered when there is a problem loading an external resource, such as an image |
resize(function) | Corresponds to the resize event, triggered when the browser window is resized |
scroll(function) | Corresponds to the scroll event, triggered when the scrollbars are used |
Using the Mouse Event Shorthand Methods
Table 9-8 describes the set of shorthand methods that jQuery provides for dealing with mouse events.
Table 9-8. Mouse Event Shorthand Methods
Method | Description |
---|---|
click(function) | Corresponds to the click event, triggered when the user presses and releases the mouse |
dblclick(function) | Corresponds to the dblclick event, triggered when the user presses and releases the mouse twice in quick succession |
focusin(function) | Corresponds to the focusin event, triggered when the element gains the focus |
focusout(function) | Corresponds to the focusout event, triggered when the element loses the focus |
hover(function)hover(function, function) | Triggered when the mouse enters or leaves an element; when one function is specified, it is used for both enter and exit events |
mousedown(function) | Corresponds to the mousedown event, triggered when the mouse button is pressed over an element |
mouseenter(function) | Corresponds to the mouseenter event, triggered when the mouse enters the region of screen occupied by an element |
mouseleave(function) | Corresponds to the mouseleave event, triggered when the mouse leaves the region of screen occupied by an element |
mousemove(function) | Corresponds to the mousemouse event, triggered when the mouse is moved within the region of screen occupied by an element |
mouseout(function) | Corresponds to the mouseout event, triggered when the mouse leaves the region of screen occupied by an element |
mouseover(function) | Corresponds to the mouseover event, triggered when the mouse enters the region of screen occupied by an element |
mouseup(function) | Corresponds to the mouseup event, triggered when the mouse button is pressed over an element |
The hover method is a convenient way of binding a handler function to the mouseenter and mouseleave events. If you provide two functions as arguments, then the first is invoked in response to the mouseenter event and the second in response to mouseleave. If you specify only one function, it will be invoked for both events. Listing 9-23 shows the use of the hover method.
Listing 9-23. Using the hover Method
...
<script type="text/javascript">
$(document).ready(function() {
$("img").hover(handleMouseEnter, handleMouseLeave);
function handleMouseEnter(e) {
$(this).css("border", "thick solid red");
};
function handleMouseLeave(e) {
$(this).css("border", "");
}
});
</script>
...
Using the Form Event Shorthand Methods
Table 9-9 describes the shorthand methods that jQuery provides for dealing with events that are usually associated with forms.
Table 9-9. Form Event Shorthand Methods
Method | Description |
---|---|
blur(function) | Corresponds to the blur event, triggered when an element loses the focus |
change(function) | Corresponds to the change event, triggered when the value of an element changes |
focus(function) | Corresponds to the focus event, triggered when an element gains the focus |
select(function) | Corresponds to the select event, triggered when the user selects the element value |
submit(function) | Corresponds to the submit event, triggered when the user submits a form |
Using the Keyboard Event Shorthand Methods
Table 9-10 describes the shorthand methods that jQuery provides for dealing with keyboard events.
Table 9-10. Keyboard Event Shorthand Methods
Method | Description |
---|---|
keydown(function) | Corresponds to the keydown event, triggered when the user presses a key |
keypress(function) | Corresponds to the keypress event, triggered when the user presses and releases a key |
keyup(function) | Corresponds to the keyup event, triggered when the user releases a key |
Summary
In this chapter, I showed you the jQuery support for events. As with much of jQuery, the benefit of the event functionality is simplicity and elegance. You can create and manage event handlers with little effort. I particularly like the support for creating live event handlers, such that elements that are added to the DOM that match a particular selector are automatically associated with event handlers. It significantly reduces the amount of time that I spend tracking down problems with event handling in my web applications. In Chapter 10, I describe the jQuery support for effects.
18.118.24.124