Click here to Skip to main content

Creating Menus using jQuery

This is an example how to create simple Menus in html using jQuery. Menus $(document).ready(function() { ...
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
See more: jQuerymenu
This is an example how to create simple Menus in html using jQuery.
 
<html>
    <head>    
        <title>Menus</title>
        <script type='text/javascript' src='jquery-1.4.min.js'></script>
        <script type='text/javascript'>
            $(document).ready(function()
            {                   
                $("#divSubMenu1,#divSubMenu2,#divSubMenu3,#divSubMenu4,#divSubMenu5").hide();
                $("#tdMenu1").mouseover(function()
                {                    
                    $("#divSubMenu1").slideDown("slow");                    
                    $("#divSubMenu2").slideUp("fast");                    
                    $("#divSubMenu3").slideUp("fast");
                    $("#divSubMenu4").slideUp("fast");
                    $("#divSubMenu5").slideUp("fast");
                });
                $("#tdMenu2").mouseover(function()
                {                    
                    $("#divSubMenu1").slideUp("fast");                    
                    $("#divSubMenu2").slideDown("slow");                    
                    $("#divSubMenu3").slideUp("fast");
                    $("#divSubMenu4").slideUp("fast");
                    $("#divSubMenu5").slideUp("fast");
                });
                $("#tdMenu3").mouseover(function()
                {                    
                    $("#divSubMenu1").slideUp("fast");                    
                    $("#divSubMenu2").slideUp("fast");                    
                    $("#divSubMenu3").slideDown("slow");
                    $("#divSubMenu4").slideUp("fast");
                    $("#divSubMenu5").slideUp("fast");
                });
                $("#tdMenu4").mouseover(function()
                {                    
                    $("#divSubMenu1").slideUp("fast");                    
                    $("#divSubMenu2").slideUp("fast");                    
                    $("#divSubMenu3").slideUp("fast");
                    $("#divSubMenu4").slideDown("slow");
                    $("#divSubMenu5").slideUp("fast");
                });
                $("#tdMenu5").mouseover(function()
                {                 
                    $("#divSubMenu1").slideUp("fast");                    
                    $("#divSubMenu2").slideUp("fast");                    
                    $("#divSubMenu3").slideUp("fast");
                    $("#divSubMenu4").slideUp("fast");
                    $("#divSubMenu5").slideDown("slow");
                });        
                
                $("#NoMenu").mouseover(function()
                {                       
                    $("#divSubMenu1,#divSubMenu2,#divSubMenu3,#divSubMenu4,#divSubMenu5").slideUp("fast");
                });
                
                $("body").click(function()
                {                       
                    $("#divSubMenu1,#divSubMenu2,#divSubMenu3,#divSubMenu4,#divSubMenu5").hide();
                });
            });
        </script>
    </head>
    <body>
        <div id="divMenu">
            <table width="100%" id="tblMenu" cellspacing="5">
                <tr style="z-index:10">
                    <td id="tdMenu1" style="width:20%">
                        <img src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                        
                    </td>
                    <td id="tdMenu2" style="width:20%">
                        <img src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                        
                    </td>
                    <td id="tdMenu3" style="width:20%">
                        <img src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                        
                    </td>
                    <td id="tdMenu4" style="width:20%">
                        <img src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                    
                    </td>
                    <td id="tdMenu5" style="width:20%">
                        <img src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                        
                    </td>
                </tr>
                <tr style="position:absolute; z-index:10">
                    <td style="width:20%;position:relative" id="tdSubMenu1">
                        <div id="divSubMenu1">
                         <table width="100%" style="border-color:Red" id="tblSubMenu1">
                            <tr>
                                <td name="tdSubMenu1">
                                    <img name="subMenu" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                               <td name="tdSubMenu1">
                                    <img name="subMenu" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                               <td name="tdSubMenu1">
                                    <img name="subMenu" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" src="Menu1.gif" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                         </table>          
                        </div>
                    </td>
                    <td style="width:20%;position:relative;" id="tdSubMenu2">
                        <div id="divSubMenu2">
                         <table width="100%" style="border-color:Red" id="tblSubMenu2">
                            <tr>
                                <td name="tdSubMenu2">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu2">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu2">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                         </table>  
                        </div>        
                    </td>
                    <td style="width:20%" id="tdSubMenu3">
                        <div id="divSubMenu3">
                         <table width="100%" style="border-color:Red" id="tblSubMenu3">
                            <tr>
                                <td name="tdSubMenu3">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu3">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu3">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                         </table>      
                        </div>    
                    </td>
                    <td style="width:20%" id="tdSubMenu4">
                        <div id="divSubMenu4">
                         <table width="100%" style="border-color:Red" id="tblSubMenu4">
                            <tr>
                                <td name="tdSubMenu4">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu4">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu4">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                         </table>  
                        </div>        
                    </td>
                    <td style="width:20%" id="tdSubMenu5">
                        <div id="divSubMenu5">
                         <table width="100%" style="border-color:Red" id="tblSubMenu5">
                            <tr>
                                <td name="tdSubMenu5">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                            <tr>
                                <td name="tdSubMenu5">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>                      
                            </tr>
                            <tr>
                                <td name="tdSubMenu5">
                                    <img src="Menu1.gif" onmouseover="this.src='Menu1_mouse.gif'" onmouseout="this.src='Menu1.gif'" width="100%" height="30px" alt="Menu1" />                                    
                                </td>
                            </tr>
                         </table>      
                        </div>    
                    </td>
                </tr>
            </table>
        </div>
        <div id="NoMenu" style="width:100%;height:100%">
            
        </div>
    </body>
</html>
Posted 5 Feb '10
Edited 5 Feb '10
fly9049K

Comments
Donsw - 2 Jul '10
Reason for my vote of 2 more description needed.
Md. Marufuzzaman - 3 Jul '10
It will be very nice to add some short clarification on what you actually doing, how your code works.
heling0223 - 5 Jul '10
Reason for my vote of 3 .
TheyCallMeMrJames - 27 Jul '10
Reason for my vote of 2 lacks description, code is verbose, no use of plugins, not demonstrative of good practices, menus not dynamic.
BYJhony - 20 Jul '11
Reason for my vote of 5 bueno

Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Alternative 4

Although this tip will work, I certainly would not recommend using it as base for more code, it is pretty inefficient and quite brittle
 
There are lots of really great jquery plugins that will automate all this for you.
 
If you really want to do this yourself, here a quick snippet that I just wrote, to try to help rather than to just be a critic It can be improved.
 
Here is a real simple clean xhtml layout structure. It has nested ul that reflect the semantic structure of the dropdown menus. This also allows for easy selection of child menus.
 
The sub menus are hidden with the class display-none which is just a wrapper around a css display:none declaration. The rest of the css is purly for layout and styling.
 
<style>
        .dropdown-menu{width:100%;}
        .dropdown-menu li{display:inline-block;
                vertical-align:top;background-color:Teal;color:white;padding-bottom:5px;}
        .dropdown-menu li ul li {display:list-item;
                background-color:Red;color:Navy;padding-bottom:3px;}
        .display-none{ display:none;}
    </style>
 

 
    <ul class="dropdown-menu">
        <li>Menu 1
            <ul class="display-none">
                <li><a href="#">sub 1</a></li>
                <li><a href="#">sub 2</a></li>
            </ul>
        </li>
        <li>Menu 2
            <ul class="display-none">
                <li><a href="#">Sub 1</a></li>
                <li><a href="#">Sub 2</a></li>
            </ul>
        </li>
    </ul>
 
 
The jQuery again is pretty simple. I have wrapped it a closue and made it a jQuery plug-in. You do not have to do this but it provides more options for only a few extra lines of javascript.
 
<script type="text/javascript">
//Create a closure to prevent conflicts with outer scripts
 
(function($) {
 

//Create a plugin by use the $.fn function to extend the jQuery object
        //Note: I have allowed options to be passed in but I am not using them
       
        $.fn.DropDownMenu = function(options) {
           
       //standard plugin stuff to allow for method chain of
        return this.each(function() {
                
                //Grab the top level dropdown menu
                var $dropdownmenu = $(this);
                
                //On all the first level li items attach a mouseover and mouseout event.
                //because we nest the sub menus with in the parent li's they will expand and
                //meaning mouseout events will be caught for submenus as well
                
                 $(">li", $dropdownmenu).mouseover(function() {
                    var $menuItem = $(this);
                    
                    //Display the submenu
                    $("ul", $menuItem).stop().slideDown("slow");
                }).mouseout(function() {
                    
                    //Do the same for mouseover but in reverse
                    var $menuItem = $(this);
                    $("ul", $menuItem).stop().slideUp("slow");
                });
            });
        };
 
})(jQuery);
 
</script>
 
To initalise the menu, use a document.ready event on you page.
<script type="text/javascript">
 
$(function() {
    $(".dropdown-menu").DropDownMenu();
});
</script>
The nice this is that becuase this is a plugin, if we have multiple dropdown menus we can given them both the css class dropdown-menu and with this one call they will both be initalised
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. Wonde Tadesse (1,220)
  2. SAKryukov (491)
  1. SAKryukov (9,712)
  2. Christian Graus (6,009)
  3. OriginalGriff (4,736)
  4. thatraja (4,349)
  5. Abhinav S (4,335)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --

Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 7 Jul 2010
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid