YUI Library Home

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.

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:

1
2    fn: Function (Required),    // The handler to call when the event fires. 
3    obj: Object (Optional), // An object to pass back to the handler. 
4    scope: Object (Optional)    // The object to use for the scope of the handler. (By default the scope is the YAHOO.widget.MenuItem instance) 
5
view plain | print | ?

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.

1/*
2     "click" event handler for each MenuItem instance - used to log 
3     info about the MenuItem that was clicked.
4*/ 
5 
6function onMenuItemClick(p_sType, p_aArgs, p_oValue) { 
7 
8    YAHOO.log(("index: " + this.index +  
9               ", text: " + this.cfg.getProperty("text") +  
10               ", value: " + p_oValue), "info""example9"); 
11 
12
13 
14 
15/*
16    Instantiate a Menu:  The first argument passed to the constructor
17    is the id for the Menu element to be created, the second is an 
18    object literal of configuration properties.
19*/ 
20 
21var oMenu = new YAHOO.widget.Menu("mymenu", { fixedcenter: true }); 
22 
23 
24/*
25    Add items to the Menu instance by passing an array of object literals 
26    (each of which represents a set of YAHOO.widget.MenuItem 
27    configuration properties) to the "addItems" method.
28*/ 
29 
30oMenu.addItems([ 
31 
32    //  Register a "click" event handler for the first item. 
33 
34    { text: "Item One", onclick: { fn: onMenuItemClick } }, 
35 
36 
37    /*
38         Register a "click" event handler for the second item, 
39         passing a string arguemnt ("foo") to the event handler.
40    */ 
41    { text: "Item Two", onclick: { fn: onMenuItemClick, obj: "foo" } }, 
42 
43 
44    /*
45         Register a "click" event handler for the third item and
46         passing and array as an argument to the event handler.
47    */ 
48    { text: "Item Three", onclick: { fn: onMenuItemClick, obj: ["foo""bar"] } } 
49 
50]); 
51 
52 
53/*
54    Since this Menu instance is built completely from script, call the 
55    "render" method passing in the DOM element that it should be 
56    appended to.
57*/ 
58 
59oMenu.render("rendertarget"); 
view plain | print | ?

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:

Logger Console

INFO 1041ms (+0) 12:37:34 AM:

MenuManager

MenuItem yui-gen2 successfully registered.

INFO 1041ms (+0) 12:37:34 AM:

Menu mymenu

Item added. Text: Item Three, Index: 2, Group Index: 0

INFO 1041ms (+0) 12:37:34 AM:

MenuManager

MenuItem yui-gen1 successfully registered.

INFO 1041ms (+0) 12:37:34 AM:

Menu mymenu

Item added. Text: Item Two, Index: 1, Group Index: 0

INFO 1041ms (+0) 12:37:34 AM:

MenuManager

MenuItem yui-gen0 successfully registered.

INFO 1041ms (+1) 12:37:34 AM:

Menu mymenu

Item added. Text: Item One, Index: 0, Group Index: 0

INFO 1040ms (+1) 12:37:34 AM:

MenuManager

Menu mymenu successfully registered.

INFO 1039ms (+0) 12:37:34 AM:

MenuManager

DOM event handlers initialized.

INFO 1039ms (+3) 12:37:34 AM:

Menu mymenu

No source element found. Created element with id: mymenu

INFO 1036ms (+1036) 12:37:34 AM:

LogReader instance0

LogReader initialized

INFO 0ms (+0) 12:37:33 AM:

global

Logger initialized

Menu Family Examples:

More Menu Family Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

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