YUI Library Examples: Menu Family: Listening For Menu Events

Menu Family: Listening For Menu Events

This example demonstrates how to listen for DOM-related events. Interaction with the Menu will result in event information being output to the console. Please note: Disabled MenuItem instances do not fire DOM events. This is demonstrated with the MenuItem named "MenuItem 2."

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 DOM-based event handlers to Menu and MenuItem instances.

All of the events for Menu and MenuItem (including DOM-based events such as "mouseover" or "click") are implemented as instances of Custom Event (YAHOO.util.CustomEvent). Please note: To listen for DOM-based events always use the provided Custom Event-based interface rather than attaching handlers directly to a Menu's DOM elements.

Add event listeners through the subscribe method. The following example demonstrates how to subscribe to the "show" and "hide" events:

1/*
2     Generic event handler used to log info about the DOM events for 
3     the Menu instance.
4*/ 
5 
6function onMenuEvent(p_sType, p_aArgs) { 
7 
8    var oDOMEvent = p_aArgs[0]; 
9 
10    YAHOO.log(("Id: " + this.id + ", " + 
11               "Custom Event Type: " + p_sType + ", " +                   
12               "DOM Event Type: " + oDOMEvent.type),  
13               "info""example10"); 
14
15 
16 
17/*
18     Generic event handler used to log info about the DOM events for 
19     each MenuItem instance.
20*/ 
21 
22function onMenuItemEvent(p_sType, p_aArgs) { 
23 
24    var oDOMEvent = p_aArgs[0]; 
25 
26    YAHOO.log(("Index: " + this.index + ", " + 
27               "Group Index: " + this.groupIndex + ", " + 
28               "Custom Event Type: " + p_sType + ", " +                   
29               "DOM Event Type: " + oDOMEvent.type 
30               ), "info""example10"); 
31     
32
33 
34 
35/*
36     Instantiate a Menu:  The first argument passed to the 
37     constructor is the id of the element in the page 
38     representing the Menu; the second is an object literal 
39     of configuration properties.
40*/ 
41 
42var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true }); 
43 
44 
45// Subscribe to the Menu instance's "itemAdded" event 
46 
47oMenu.subscribe("itemAdded"function (p_sType, p_aArgs) { 
48 
49    var oMenuItem = p_aArgs[0]; 
50     
51    /*
52         Subscribe to each MenuItem instance's DOM events as they
53         are added to the Menu instance.
54    */ 
55 
56    oMenuItem.subscribe("mouseover", onMenuItemEvent); 
57    oMenuItem.subscribe("mouseout", onMenuItemEvent); 
58    oMenuItem.subscribe("mousedown", onMenuItemEvent); 
59    oMenuItem.subscribe("mouseup", onMenuItemEvent); 
60    oMenuItem.subscribe("click", onMenuItemEvent); 
61    oMenuItem.subscribe("keydown", onMenuItemEvent); 
62    oMenuItem.subscribe("keyup", onMenuItemEvent); 
63    oMenuItem.subscribe("keypress", onMenuItemEvent); 
64 
65}); 
66 
67 
68//  Subscribe to every DOM event for the Menu instance. 
69 
70oMenu.subscribe("mouseover", onMenuEvent); 
71oMenu.subscribe("mouseout", onMenuEvent); 
72oMenu.subscribe("mousedown", onMenuEvent); 
73oMenu.subscribe("mouseup", onMenuEvent); 
74oMenu.subscribe("click", onMenuEvent); 
75oMenu.subscribe("keydown", onMenuEvent); 
76oMenu.subscribe("keyup", onMenuEvent); 
77oMenu.subscribe("keypress", onMenuEvent); 
78 
79 
80/*
81    Add items to the Menu instance by passing an array of object literals 
82    (each of which represents a set of YAHOO.widget.MenuItem 
83    configuration properties) to the "addItems" method.
84*/ 
85 
86oMenu.addItems([ 
87 
88        "MenuItem 0"
89 
90        "MenuItem 1"
91 
92        /*
93             Add a disabled menu item to demonstrate that disabled 
94             items do not respond to DOM events.
95        */ 
96        { text: "MenuItem 2", disabled: true }, 
97 
98        "MenuItem 3"
99 
100        "MenuItem 4" 
101 
102    ]); 
103 
104 
105/*
106    Since this Menu instance is built completely from script, call the 
107    "render" method passing in the DOM element that it should be 
108    appended to.
109*/ 
110 
111oMenu.render("rendertarget"); 
view plain | print | ?

YUI Logger Output:

Logger Console

INFO0ms (+0) 5:34:44 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