YUI Library Examples: Menu Family: Handling Menu Click Events

Menu Family: Handling Menu Click Events

This example demonstrates how to register a "click" event handler for a MenuItem instance.

Note: By default clicking outside of a menu will hide it. Additionally, menu items without a submenu or a URL to navigate will hide their parent menu when clicked. Click the "Show Menu" button below to make the menu visible if it is hidden.

Adding "click" event handlers to items in a Menu

The easiest way to add a "click" event handler to an item in a Menu is to define it via the "onclick" configuration property when adding the item to its parent Menu. The "onclick" configuration property accepts an object literal representing the code to be executed when the menu item is clicked. The format for the object literal is:
{
fn: Function (Required), // The handler to call when the event fires.
obj: Object (Optional), // An object to pass back to the handler.
scope: Object (Optional) // The object to use for the scope of the handler. (By default the scope is the YAHOO.widget.MenuItem instance)
}

The handler that is called when the "click" event fires recieves two arguments: a string representing the name of the event, and an array of arguments sent when the event was fired. The first item in the arguments array is the actual DOM event representing the click. If a value was specified for the "obj" property of the object literal defined for the "onclick" configuration property, it will be passed back as the third argument to the function specified as the "click" event handler.

Copyright © 2007 Yahoo! Inc. All rights reserved.

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