Yahoo! Developer Network Home - Help

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 
17     constructor is the id of the element in the page 
18     representing the Menu; the second is an object literal 
19     of configuration properties.
20*/ 
21 
22var oMenu = new YAHOO.widget.Menu("mymenu", { fixedcenter: true }); 
23 
24 
25/*
26    Add items to the Menu instance by passing an array of object literals 
27    (each of which represents a set of YAHOO.widget.MenuItem 
28    configuration properties) to the "addItems" method.
29*/ 
30 
31oMenu.addItems([ 
32 
33    //  Register a "click" event handler for the first item. 
34 
35    { text: "Item One", onclick: { fn: onMenuItemClick } }, 
36 
37 
38    /*
39         Register a "click" event handler for the second item, 
40         passing a string arguemnt ("foo") to the event handler.
41    */ 
42    { text: "Item Two", onclick: { fn: onMenuItemClick, obj: "foo" } }, 
43 
44 
45    /*
46         Register a "click" event handler for the third item and
47         passing and array as an argument to the event handler.
48    */ 
49    { text: "Item Three", onclick: { fn: onMenuItemClick, obj: ["foo""bar"] } } 
50 
51]); 
52 
53 
54/*
55    Since this Menu instance is built completely from script, call the 
56    "render" method passing in the DOM element that it should be 
57    appended to.
58*/ 
59 
60oMenu.render("rendertarget"); 
view plain | print | ?

YUI Logger Output:

Logger Console

INFO0ms (+0) 11:41:08 AM:

global

Logger initialized

Menu Family Examples:

More Menu Family Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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