Click here to Skip to main content
13,353,322 members (51,577 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 29 Jun 2012

How to Create a Menu Using JavaScript

, 29 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Create a menu using JavaScript on mouseover and hide this menu on mouse out event.


In this article you learn how to create a menu using JavaScript on mouseover and hide this menu on mouse out event.

How to Create a Menu

First of all we need to add a style for a div which contains the menu and then for the table's column td.

.DivMenu {
      background-color: darkorange;
      border: 1px groove lightgrey;
   .TDMenu {

In the ASPX page you can add any control on which you want to show a menu on mouseover event and hide on the mouseout event. Then you create a div and in this div use a table, and on both controls (div and table's td) apply the style class.

<div id="JsMenuDiv" class="DivMenu" onmouseout="return hideMenu(this)">
<TD id="MenuSaveAs" RollOver RollOut onclick="goPage('Default4.aspx')" class="TDMenu">

In td, we use Rollover RollOut. We use it in the JavaScript function; on the mouseover and mouseout events of this td, we change the color.

function document_onmouseover()
   var srcElement = event.srcElement;
   if (srcElement.tagName == "TD" && typeof(srcElement.RollOver) != "undefined")
   { = "white"; ="darkblue";
function document_onmouseout()
   var srcElement = event.srcElement;
   if (srcElement.tagName == "TD" && typeof(srcElement.RollOut) != "undefined")
   { = "darkblue"; = "darkorange";

document.onmouseover = document_onmouseover;
document.onmouseout = document_onmouseout;
Figure 1. When mouse over then backcolor change

Now we add two functions for showing and hiding menu on mouseover and mouseout events.

function showMenu(menuToShow)
   var srcElement = event.srcElement;
   var xPos = parseInt(srcElement.offsetLeft);
   var yPos = parseInt(srcElement.offsetTop); = xPos + (srcElement.width) = yPos+40;

function hideMenu(menuToHide)
   if (event.toElement != menuToHide &&
      menuToHide.contains(event.toElement) == false)
   { = -200; = -1000;
Figure 2. When mouse over on Button then show menu


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


About the Author

Software Developer (Senior)
India India
i work in ASP.Net,C#,Sql,VB6.0 etc. I want to enhance my skills by learning new technologies.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Prosan29-Jun-12 19:42
memberProsan29-Jun-12 19:42 
Bug;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P;P Pin
NarangGopal29-Jun-12 10:22
memberNarangGopal29-Jun-12 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 29 Jun 2012
Article Copyright 2012 by Prosan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid