YUI Library Examples: Event Utility: Simple Event Handling and Processing

Event Utility: Simple Event Handling and Processing

Clicking in the blue box will pop up a "Hello World!" alert window. Clicking on the first link will take you to the YUI website; clicking on the second link, which has the same href attribute, will pop up an alert instead and not navigate to a new page.

Event Utility is used here to do two things:

  1. Attach the click event handler to the blue box;
  2. Attach an event handler to the second <a> element that uses YAHOO.util.Event.preventDefault() to prevent the link, when clicked, from navigating to a new page.

Click for Hello World alert.

The YUI Library. (Link navigates away from page.)

The YUI Library. (Link's default behavior is suppressed.)

Making Use of Event Utility's Basic Features

The YUI Event Utility is a simple, powerful resource for creating event-driven applications in the browser. In this very simple example, we'll look at how to use Event Utility to listen for a specific event on a specific element. We'll also look at how Event Utility can be used within an event handler to provide additional control.

To illustrate event handling syntax, we'll create a <div> and pop up an alert message when that <div> is clicked on. Begin with the style and markup necessary to make your element visible:

1<style type="text/css"
2#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;} 
3</style> 
4 
5<div id="container"
6  <p>Click for Hello World alert.</p> 
7</div> 
view plain | print | ?

Next, create a function that receives a single argument — the event object — and pops up an alert which says "Hello World!":

1//A function that pops up a "Hello World" alert: 
2var helloWorld = function(e) { 
3    alert("Hello World!"); 
4
view plain | print | ?

With our markup on the page and a function that we want to execute when our element is clicked on, we now use Event Utility's addListener method to attach our helloWorld function as a handler for the click event on the element whose HTML ID is "container":

1YAHOO.util.Event.addListener("container""click", helloWorld); 
view plain | print | ?

Almost all event handling begins with a premise just this simple: We have an element ("container") to which something might happen (eg, it might be clicked) and, when that does happen, we want to do something (eg, helloWorld).

In some cases, you may want to use some of Event Utility's powerful browser abstraction methods to help you handle your interaction flow during an event. For example, lets say you have two links on the page:

1<p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> 
2p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p> 
view plain | print | ?

Imagine that when the second link is clicked you want to pop up an alert window and then prevent the browser from navigating to the page designated in the href attribute. The event object has a preventDefault mechanism, but that mechanism was not successfully implemented across all A-Grade browsers until quite recently. So, instead of using the built-in version of preventDefault, we can use Event Utility's method which features normalized support for a greater number of browsers:

1//A function that pops up an alert and 
2//prevents the default behavior of the event 
3//for which it is a handler: 
4var interceptLink = function(e) { 
5    YAHOO.util.Event.preventDefault(e); 
6    alert("You clicked on the second YUI link."); 
7
8 
9//subscribe our interceptLink function 
10//as a click handler for the second anchor 
11//element: 
12YAHOO.util.Event.addListener("secondA""click", interceptLink); 
view plain | print | ?

In line 5 above, we take the event object, passed into us by the Event Utility when the handler is called, and we apply the Event Utility's preventDefault method to it. We can use a similar pattern for all of Event Utility's helper methods (stopEvent, stopPropagation, etc.).

YUI Logger Output:

Logger Console

INFO 2ms (+1) 7:00:16 PM:

example:

When you begin interacting with the example at left, you'll see log messages appear here.

INFO 1ms (+1) 7:00:16 PM:

global:

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More Event Utility Resources:

Copyright © 2007 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings