YUI recommends YUI3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

This documentation is no longer maintained.

YUI Library Examples: Menu Family: Basic Menu From JavaScript

Menu Family: Basic Menu From JavaScript

This example demonstrates how to create a Menu instance using nothing but JavaScript.

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.

Creating a Menu from JavaScript

To create a Menu with no pre-existing markup on the page, call the Menu constructor (YAHOO.widget.Menu) passing the id of the Menu element to be created as the first argument.

Add items to a Menu instance via the addItem, insertItem, or addItems methods.

Finally, it is necessary to call the render method passing the id of, or reference to the element the Menu should be appended to.

1/*
2    Instantiate a Menu:  The first argument passed to the constructor
3    is the id for the Menu element to be created, the second is an 
4    object literal of configuration properties.
5*/ 
6 
7var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true }); 
8 
9 
10/*
11    Add items to the Menu instance by passing an array of object literals 
12    (each of which represents a set of YAHOO.widget.MenuItem 
13    configuration properties) to the "addItems" method.
14*/ 
15 
16oMenu.addItems([ 
17 
18        { text: "Yahoo! Mail", url: "http://mail.yahoo.com" }, 
19        { text: "Yahoo! Address Book", url: "http://addressbook.yahoo.com" }, 
20        { text: "Yahoo! Calendar", url: "http://calendar.yahoo.com" }, 
21        { text: "Yahoo! Notepad",  url: "http://notepad.yahoo.com" } 
22 
23    ]); 
24 
25 
26/*
27    Since this Menu instance is built completely from script, call the 
28    "render" method passing in the DOM element that it should be 
29    appended to.
30*/ 
31 
32oMenu.render("rendertarget"); 
view plain | print | ?

Menu Family Examples:

More Menu Family Resources:

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings