YUI Library Home

Site Search (YDN & YUIBlog):

# 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 instance will hide it. Additionally, MenuItem instances without a submenu or a URL to navigate to will hide their parent Menu instance when clicked. Click the "Show Menu" button below to make the Menu instance visible if it is hidden.

View example in new window.

## Adding "click" event handlers to items in a Menu

The "onclick" configuration property provides an easy way define a "click" event listener for individual items when building menus from script. The "onclick" configuration property accepts an object literal representing the code to be executed when the MenuItem instance 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.

/* "click" event handler for each MenuItem instance - used to log info about the MenuItem that was clicked. */ function onMenuItemClick(p_sType, p_aArgs, p_oValue) { YAHOO.log(("index: " + this.index + ", text: " + this.cfg.getProperty("text") + ", value: " + p_oValue), "info", "example9"); } /* Instantiate a Menu: The first argument passed to the constructor is the id for the Menu element to be created, the second is an object literal of configuration properties. */ var oMenu = new YAHOO.widget.Menu("mymenu", { fixedcenter: true }); /* Add items to the Menu instance by passing an array of object literals (each of which represents a set of YAHOO.widget.MenuItem configuration properties) to the "addItems" method. */ oMenu.addItems([ // Register a "click" event handler for the first item. { text: "Item One", onclick: { fn: onMenuItemClick } }, /* Register a "click" event handler for the second item, passing a string arguemnt ("foo") to the event handler. */ { text: "Item Two", onclick: { fn: onMenuItemClick, obj: "foo" } }, /* Register a "click" event handler for the third item and passing and array as an argument to the event handler. */ { text: "Item Three", onclick: { fn: onMenuItemClick, obj: ["foo", "bar"] } } ]); /* Since this Menu instance is built completely from script, call the "render" method passing in the DOM element that it should be appended to. */ oMenu.render("rendertarget");

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

### Menu Family Examples:

- Basic Menu From Markup
- Basic Menu From JavaScript
- Grouped Menu Items Using Markup
- Grouped Menu Items Using JavaScript
- Grouped Menu Items With Titles From Markup
- Grouped Menu Items With Titles From JavaScript
- Multi-tiered Menu From Markup
- Multi-tiered Menu From JavaScript
- Handling Menu Click Events
- Listening For Menu Events
- MenuItem Configuration Properties
- Setting Menu Configuration Properties At Runtime
- Website Left Nav With Submenus Built From Markup
- Website Left Nav With Submenus From JavaScript
- Website Top Nav With Submenus Built From Markup
- Website Top Nav With Submenus From JavaScript
- Website Left Nav Using Animation With Submenus Built From Markup
- Website Left Nav Using Animation With Submenus From JavaScript
- Website Top Nav Using Animation With Submenus Built From Markup
- Website Top Nav Using Animation With Submenus From JavaScript
- Context Menu
- Adding A Context Menu To A Table
- Adding A Context Menu To A TreeView
- OS-Style Programs Menu
- Application Menubar
- Using the Menu ARIA Plugin
- Replacing the content of a Button's Menu (included with examples for Button Control)
- Using A Menu Button To Replace A <select> Element (included with examples for Button Control)
- Using the Button ARIA Plugin (included with examples for Button Control)
- Context Menu Integration (included with examples for DataTable Control)
- Menu Buttons (included with examples for Button Control)
- Split Buttons (included with examples for Button Control)
- Color Picker Button (included with examples for Button Control)
- Fixed Width Menu Button (included with examples for Button Control)
- Slider Button (included with examples for Button Control)
- Layout with Menu Controls (included with examples for Layout Manager)
- Using the ProfilerViewer API while Profiling the YUI Menu Control (included with examples for ProfilerViewer Control)

### More Menu Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

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