DropDownExtender With JavaScript

Step 1: Here we create a simple example of a DropDownExtender with JavaScript. In this example, the DropDownBox is shown when we MouseOver on a div.

DropDown1.gif

Now we write the JavaScript function show():
  1. <script language="JavaScript" type="text/javascript" >  
  2.         function show() {  
  3.             document.getElementById('divmain').style.backgroundColor = "Red";  
  4.             document.getElementById('a1').style.display = 'block';  
  5.         }  
  6.      
  7. </script> 
DropDown2.gif

Here we set the background color of the div (divmain) and show the a1.

Step 2: When we click on the a1 the following submenu will show:
 
DropDown3.gif

  1. <a id="a1" onmouseover="show()"   
  2.             style="background-color:Fuchsia; display:none;" onclick="ShowSubmenu()">v</a>  
  3. Here the ShowSubmenu() function is used to display the Submenu:  
  4. <table cellpadding="0" cellspacing="0" onmouseover="ShowSubmenu()" onmouseout="HideSubMenu()">  
  5.     <tr><td id="td1" bgcolor="#FFCC99" class="style3"  onmouseout="hidestyletd1()" onmouseover="showstyletd1()" onclick="onclicktd1()" style="border-width: thin; border-style: solid; text-align: center; display:none">Mahak</td></tr>  
  6.     <tr><td id="td2" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd2()" onmouseover="showstyletd2()" onclick="onclicktd2()"  
  7.             style="border-width: thin; border-style: solid; text-align: center; display:none">Pihu</td></tr>  
  8.     <tr><td id="td3" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd3()" onmouseover="showstyletd3()"  onclick="onclicktd3()"  
  9.             style="border-width: thin; border-style: solid; text-align: center; display:none">Juhi</td></tr>  
  10.     </table> 
Here we set the display property of td1,td2,td3 is None.

Now we write the ShowSubmenu function in Javascript:
  1. function ShowSubmenu() {  
  2.             document.getElementById('td1').style.display = 'block';  
  3.             document.getElementById('td2').style.display = 'block';  
  4.             document.getElementById('td3').style.display = 'block';  
  5.         } 
Here we set the display property of td to 'block'.

The other function HideSubMenu() is used to hide the SubMenu when we click outside the submenu:
  1. function HideSubMenu() {  
  2.             document.getElementById('td1').style.display = 'none';  
  3.             document.getElementById('td2').style.display = 'none';  
  4.             document.getElementById('td3').style.display = 'none';  
  5.    
  6.         } 
Step 3: Now we change the background color and forecolor of the submenu with the help of javascript:
  1. <tr><td id="td1" bgcolor="#FFCC99" class="style3"  onmouseout="hidestyletd1()" onmouseover="showstyletd1()" onclick="onclicktd1()" style="border-width: thin; border-style: solid; text-align: center; display:none">Mahak</td></tr>  
  2.     <tr><td id="td2" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd2()" onmouseover="showstyletd2()" onclick="onclicktd2()"  
  3.             style="border-width: thin; border-style: solid; text-align: center; display:none">Pihu</td></tr>  
  4.     <tr><td id="td3" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd3()" onmouseover="showstyletd3()"  onclick="onclicktd3()"  
  5.             style="border-width: thin; border-style: solid; text-align: center; display:none">Juhi</td></tr> 
Here the showstyle functions are used to change the colors on the onmouse over the event and the hidestyle is used not to change the color means it sets the default color:
  1. function showstyletd1() {  
  2.             document.getElementById('td1').style.backgroundColor = "Pink";  
  3.             document.getElementById('td1').style.color = "Blue";  
  4.         }  
  5.         function hidestyletd1() {  
  6.             document.getElementById('td1').style.backgroundColor = "#FFCC99";  
  7.             document.getElementById('td1').style.color = "Black";  
  8.         }  
  9.         function showstyletd2() {  
  10.             document.getElementById('td2').style.backgroundColor = "Pink";  
  11.             document.getElementById('td2').style.color = "Blue";  
  12.         }  
  13.         function hidestyletd2() {  
  14.             document.getElementById('td2').style.backgroundColor = "#FFCC99";  
  15.             document.getElementById('td2').style.color = "Black";  
  16.         }  
  17.         function showstyletd3() {  
  18.             document.getElementById('td3').style.backgroundColor = "Pink";  
  19.             document.getElementById('td3').style.color = "Blue";  
  20.         }  
  21.         function hidestyletd3() {  
  22.             document.getElementById('td3').style.backgroundColor = "#FFCC99";  
  23.             document.getElementById('td3').style.color = "Black";  
  24.         } 
DropDown4.gif

Step 4: Now if we want to display the Name like this:

DropDown5.gif

There is a Label (lblname) Control to display the name on the click of the SubMenu.
  1. <asp:Label ID="lblname" runat="server" Text="" style="font-weight: 700"></asp:Label>  
Here we write the following Code:
  1. <tr><td id="td1" bgcolor="#FFCC99" class="style3"  onmouseout="hidestyletd1()" onmouseover="showstyletd1()" onclick="onclicktd1()" style="border-width: thin; border-style: solid; text-align: center; display:none">Mahak</td></tr>  
  2.     <tr><td id="td2" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd2()" onmouseover="showstyletd2()" onclick="onclicktd2()"  
  3.             style="border-width: thin; border-style: solid; text-align: center; display:none">Pihu</td></tr>  
  4.     <tr><td id="td3" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd3()" onmouseover="showstyletd3()"  onclick="onclicktd3()"  
  5.             style="border-width: thin; border-style: solid; text-align: center; display:none">Juhi</td></tr> 
Now we can write the Javascript function:
  1. function onclicktd1()  
  2.         {  
  3.         document.getElementById('lblname').innerHTML = "Mahak";  
  4.         }  
  5.         function onclicktd2()  
  6.         {  
  7.         document.getElementById('lblname').innerHTML = "Pihu";  
  8.         }  
  9.         function onclicktd3()  
  10.         {  
  11.         document.getElementById('lblname').innerHTML = "Juhi";  
  12.         } 
Here we set the innerHTML property of the Label (lblname).

Complete Code:
  1. <head runat="server">  
  2.     <title></title>  
  3.     <script language="JavaScript" type="text/javascript" >  
  4.         function show() {  
  5.             document.getElementById('divmain').style.backgroundColor = "Red";  
  6.             document.getElementById('a1').style.display = 'block';  
  7.         }  
  8.         function ShowSubmenu() {  
  9.             document.getElementById('td1').style.display = 'block';  
  10.             document.getElementById('td2').style.display = 'block';  
  11.             document.getElementById('td3').style.display = 'block';  
  12.         }  
  13.         function HideSubMenu() {  
  14.             document.getElementById('td1').style.display = 'none';  
  15.             document.getElementById('td2').style.display = 'none';  
  16.             document.getElementById('td3').style.display = 'none';  
  17.         }  
  18.         function showstyletd1() {  
  19.             document.getElementById('td1').style.backgroundColor = "Pink";  
  20.             document.getElementById('td1').style.color = "Blue";  
  21.         }  
  22.         function hidestyletd1() {  
  23.             document.getElementById('td1').style.backgroundColor = "#FFCC99";  
  24.             document.getElementById('td1').style.color = "Black";  
  25.         }  
  26.         function showstyletd2() {  
  27.             document.getElementById('td2').style.backgroundColor = "Pink";  
  28.             document.getElementById('td2').style.color = "Blue";  
  29.         }  
  30.         function hidestyletd2() {  
  31.             document.getElementById('td2').style.backgroundColor = "#FFCC99";  
  32.             document.getElementById('td2').style.color = "Black";  
  33.         }  
  34.         function showstyletd3() {  
  35.             document.getElementById('td3').style.backgroundColor = "Pink";  
  36.             document.getElementById('td3').style.color = "Blue";  
  37.         }  
  38.         function hidestyletd3() {  
  39.             document.getElementById('td3').style.backgroundColor = "#FFCC99";  
  40.             document.getElementById('td3').style.color = "Black";  
  41.         }  
  42.         function onclicktd1()  
  43.         {  
  44.         document.getElementById('lblname').innerHTML = "Mahak";  
  45.         }  
  46.         function onclicktd2()  
  47.         {  
  48.         document.getElementById('lblname').innerHTML = "Pihu";  
  49.         }  
  50.         function onclicktd3()  
  51.         {  
  52.         document.getElementById('lblname').innerHTML = "Juhi";  
  53.         }  
  54.     </script>  
  55.     <style type="text/css">  
  56.         .style1  
  57.         {  
  58.             width: 199px;  
  59.         }  
  60.         .style2  
  61.         {  
  62.             width: 23px;  
  63.             text-align: center;  
  64.         }  
  65.         .style3  
  66.         {  
  67.             height: 19px;  
  68.             width: 209px;  
  69.         }  
  70.         .style4  
  71.         {  
  72.             text-align: center;  
  73.             width: 209px;  
  74.         }  
  75.     </style>  
  76. </head>  
  77. <body>  
  78.     <form id="form1" runat="server">  
  79.     <div>  
  80.     <table cellpadding="0" cellspacing="0">  
  81.     <tr>  
  82.     <td class="style1">  
  83.     <div id="divmain" onmouseover="show()"   onmouseout="HideSubMenu()"> Select your name here </div>  
  84.     </td>  
  85.     <td class="style2">  
  86.     <a id="a1" onmouseover="show()"   
  87.             style="background-color:Fuchsia; display:none;"onclick="ShowSubmenu()">v</a>  
  88.     </td>  
  89.     </tr>  
  90.     </table>  
  91.     <table cellpadding="0" cellspacing="0" onmouseover="ShowSubmenu()" onmouseout="HideSubMenu()">  
  92.     <tr><td id="td1" bgcolor="#FFCC99" class="style3"  onmouseout="hidestyletd1()" onmouseover="showstyletd1()" onclick="onclicktd1()" style="border-width: thin; border-style: solid; text-align: center; display:none">Mahak</td></tr>  
  93.     <tr><td id="td2" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd2()" onmouseover="showstyletd2()" onclick="onclicktd2()"  
  94.             style="border-width: thin; border-style: solid; text-align: center; display:none">Pihu</td></tr>  
  95.     <tr><td id="td3" bgcolor="#FFCC99" class="style4" onmouseout="hidestyletd3()" onmouseover="showstyletd3()"  onclick="onclicktd3()"  
  96.             style="border-width: thin; border-style: solid; text-align: center; display:none">Juhi</td></tr>  
  97.     </table>  
  98.     Welcome   
  99.         :    
  100.         <asp:Label ID="lblname" runat="server" Text="" style="font-weight: 700"></asp:Label>  
  101.     </div>  
  102.     </form>  
  103. </body>  
  104.