Create a Simple DropDownListBox In Javascript

Introduction

 
In this example we will discuss how to create a simple DropDownListBox in JavaScript like this:
 
DropDown1.jpg

DropDown2.jpg
 
DropDown3.jpg
 
Step 1
 
First, we create a table in which we will use the following items:
  1. <table cellpadding="0" cellspacing="0" border="1">  
  2.     <tr>  
  3.         <td width="250">  
  4.             <p id="p1" align="Center">  
  5.                 Select Item</p>  
  6.         </td>  
  7.         <td>  
  8.             <img src="Down Arrow.png" onclick="document.getElementById('MYList').style.display='block';"  
  9.                 height="20" />  
  10.         </td>  
  11.     </tr>  
  12.     <tr>  
  13.         <td>  
  14.             <table id="MYList" align="Center">  
  15.             </table>  
  16.         </td>  
  17.         <tr>  
  18. </table> 

DropDown4.jpg
 
Here we will use the <p> tag to show our selected item and an image, so when we click on that image the list will be shown and here we use a <table> to show the items in the list.

Step 2
 
Now we will use a TextBox and a Button Control to add our items in the Listbox:
  1. <input type="textbox" id="txt1" />  
  2. <input type="button" value="Add Items In The List" onclick="addMyList('MYList')" /> 
DropDown5.jpg

So when we click on the button the items will be added in the List. For this, we will write the JavaScript function like this:
  1. <script language="javascript">  
  2.     function addMyList(myTable) {  
  3.         document.getElementById('MYList').style.display = 'block';  
  4.         var table = document.getElementById(myTable);  
  5.         var rowCount = table.rows.length;  
  6.         var row = table.insertRow(rowCount);  
  7.         var b = document.getElementById('txt1').value;  
  8.         var cell1 = row.insertCell(0);  
  9.         cell1.style.textAlign = "center";  
  10.         var element1 = document.createElement("p");  
  11.         element1.style.textAlign = "center";  
  12.         var node = document.createTextNode(b);  
  13.         element1.appendChild(node);  
  14.         element1.onmouseover = function () {  
  15.             this.style.backgroundColor = "Red"this.style.width = "250"this.style.textAlign = "center";  
  16.         };  
  17.         element1.onmouseout = function () {  
  18.             this.style.backgroundColor = "White"this.style.width = "250"this.style.textAlign = "center";  
  19.         };  
  20.         element1.onclick = function () {  
  21.             document.getElementById('p1').innerHTML = b;  
  22.             document.getElementById('MYList').style.display = 'none';  
  23.    
  24.    
  25.         };  
  26.         cell1.appendChild(element1);  
  27.         document.getElementById('txt1').innerHTML = "";  
  28.    
  29.     }  
  30. </script> 
Here we first dynamically create a table and add a row in it, in this row we create a cell in which we create an element of the <p> tag like this:
  1. var table = document.getElementById(myTable);  
  2. var rowCount = table.rows.length;  
  3. var row = table.insertRow(rowCount);  
  4. var b=document.getElementById('txt1').value;  
  5. var cell1 = row.insertCell(0);  
  6. cell1.style.textAlign="center";  
  7. var element1 = document.createElement("p");  
  8. element1.style.textAlign="center";  
  9. var node=document.createTextNode(b);  
  10. element1.appendChild(node); 
Here we store the TextBox value like this:
  1. var b=document.getElementById('txt1').value; 
Now we will write the events (onmouseover,onmouseout an onclick) on our element1, so when we put our mouse over on the element the background color will be Red, for this we will write the following code:
  1. element1.onmouseover = function() {  
  2.     this.style.backgroundColor = "Red";this.style.width="250";this.style.textAlign="center";  
  3. };   
So the output will be:
 
DropDown6.jpg
 
Now when we hover the mouse over it, it will return to its original color, for this we will write the following code:
  1. element1.onmouseout = function() {  
  2.     this.style.backgroundColor = "White";this.style.width="250";this.style.textAlign="center";  
  3. };  
 
DropDown7.jpg
 
Now we want that, when we click on this element, the item will be shown as the selected item, for this, we will write the following code:
  1. element1.onclick = function() {  
  2.     document.getElementById('p1').innerHTML=b;  
  3.    document.getElementById('MYList').style.display='none';  
  4. };   
So the output will be:
 
DropDown8.jpg