YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: Event: Simple DOM Events

Note: This is YUI 3.x. Looking for YUI 2.x?

## Event: Simple DOM Events

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 `preventDefault()` to prevent the link, when clicked, from navigating to a new page.

View example in new window.

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 the Event Utility's Basic Features

The YUI Event Utility is a simple, powerful resource for creating event-driven applications in the browser. In this introductory example, we'll look at how to use Event Utility to listen for a specific user event on a specific element in the DOM. 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:

<style type="text/css"> #container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;} </style> <div id="container"> <p>Click for Hello World alert.</p> </div>

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

//A function that pops up a "Hello World" alert: var helloWorld = function(e) { alert("Hello World!"); }

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 `on` method to attach our `helloWorld` function as a handler for the click event on the element whose HTML ID is `container`. `on` is available for convenience at the top-level of a YUI instance; as a result, it can be referenced as `Y.on` where Y is a YUI instance:

//subscribe the helloWorld function as an event //handler for the click event on the container //div: Y.on("click", helloWorld, "#container");

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, let's say you have two links on the page:

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

Let's say 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 native browser event object has a `preventDefault` mechanism, but that mechanism was not successfully implemented across all A-Grade browsers until recently. But the event object passed to your event handler is a facade — not the actual browser event object. On this facade, `preventDefault` is implemented consistently across browsers. As a result, we can call `preventDefault` from the event facade just as we would from a native event object and expect it to work consistently across browsers:

//A function that pops up an alert and //prevents the default behavior of the event //for which it is a handler: var interceptLink = function(e) { e.preventDefault(); Y.log("You clicked on the second YUI link.", "info", "example"); alert("You clicked on the second YUI link. Because *preventDefault* was called, this link will not navigate away from the page."); } //subscribe our interceptLink function //as a click handler for the second anchor //element: Y.on("click", interceptLink, "#secondA");

The key lesson here is that you should treat the event facade (the first argument passed to your event handler) just as you would a native event object.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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