JavaScript
  Home arrow JavaScript arrow Page 3 - Using the YUI Menu Control
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using the YUI Menu Control
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2007-05-02

    Table of Contents:
  • Using the YUI Menu Control
  • Creating a Basic Menu
  • Rendering and Displaying the Menu
  • Changing the Styling

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using the YUI Menu Control - Rendering and Displaying the Menu


    (Page 3 of 4 )

    All we need to do now is add a little more JavaScript to render and display the menu. Add the following code to the bottom of the <head> section:

    <script type="text/javascript">
     
    YAHOO.util.init = function() {
       
    var myMenu = new YAHOO.widget.Menu("mymenu");
       
    myMenu.render();
       
    myMenu.show();
     
    };
     
    YAHOO.util.Event.onAvailable("mymenu", YAHOO.util.init);
    </script>

    The YAHOO.util.init function contains the code that generates the menu. First a variable is created that holds an instance of the YAHOO.widget.Menu widget, taking the name you gave to the root div of the menu as an argument. It then calls the .render and .show methods to create and display the menu.

    Built-in events are continually firing when YUI utilities and controls are active in a browser (even if they don't appear to be actually doing anything). The custom onAvailable event will fire once the <div> set as the root of the menu has been added to the DOM (i.e. has been rendered on-screen). As soon as the element is available, its event is picked up by YAHOO.util.Event and the constructor function that creates the menu is called, generating the menu on the page. This is everything the basic menu needs to function, so if you view it in your browse, you'll see it in all its glory!

    Adding sub-menus to the menu is equally as straightforward; you just have to add a new menu (including the root and container <div> elements) to the <li> element that the sub-menu should appear under (make sure it appears before the closing </li> tag of the item the sub-menu is for). So to add a sub-menu to the About Us item, add the following code to the page:

    <li class="yuimenuitem"><a rel="nofollow" target="_blank" href = "aboutus.htm">About Us</a>
     
    <div id="aboutus" class="yuimenu">
       
    <div class="bd">
         
    <ul>
           
    <li class="yuimenuitem"><a rel="nofollow" target="_blank" href = "philosophy.htm">Our Philosophy</a></li>
           
    <li class="yuimenuitem"><a rel="nofollow" target="_blank" href = "contactus.htm">Contact Us</a></li>
           
    <li class="yuimenuitem"><a rel="nofollow" target="_blank" href = "press.htm">Press Releases</a></li>
         
    </ul>
       
    </div>
     
    </div>
    </li>

    The menu control will add the sub-menu and the default behavior (opening and closing the submenu when appropriate) to your menu, as well as adding the arrow icon indicating there is a sub menu. It should now look a little like the below screen shot:

    More JavaScript Articles
    More By Dan Wellman


       · Hi, I hope this follow-up article is a good demonstration of how to bend the YUI to...
       · try viewing this on a safari browser, it wont work properlyany idea of how to fix...
       · which version of Safari are you using and which version of OSX?
       · If I wanted to create the menu without any HTML markup, how would I go about...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek