JavaScript Wrapper Methods To Deal With HTML Dropdown Control

I thought to write some JavaScript wrapper code without using any client side framework to deal with HTML controls efficiently, with the help of method and properties, in a similar way to  how a Windows developer does in the Window form application without knowing the inner complexities.

Though jQuery framework provides lots of methods and properties to deal with the HTML controls, at some point the developer has to remember some of the attributes to supply as an argument or parameter to jQuery methods or properties. For example,  to add a new element to the dropdown or with a text value pair, or getting a value for a given index position, etc., which is not a bit of easy work. So in this attempt of writing code, I just focused on to deal with dropdown control, and I believe the reader will have enough of an idea about writing code in Java Script, and also how to write methods and properties and classes.

Here's the wrapper methods and properties:

  1. var arj = {  
  2.     //FOR DROPDOWN HTML CONTROL  
  3.     Select: function (ElementID)  
  4.     {  
  5.         this.SelectedIndex = function ()  
  6.         {  
  7.             var ctrl = document.getElementById(ElementID);  
  8.             return ctrl.selectedIndex;  
  9.         }();  
  10.         this.SelectedText = function ()  
  11.         {  
  12.             var ctrl = document.getElementById(ElementID);  
  13.             if (ctrl.selectedIndex >= 0)  
  14.             {  
  15.                 return ctrl.options[ctrl.selectedIndex].text;  
  16.             }  
  17.             else  
  18.             {  
  19.                 return "";  
  20.             }  
  21.         }();  
  22.         this.SelectedValue = function ()  
  23.         {  
  24.             var ctrl = document.getElementById(ElementID);  
  25.             if (ctrl.selectedIndex >= 0)  
  26.             {  
  27.                 return ctrl.options[ctrl.selectedIndex].value;  
  28.             }  
  29.             else  
  30.             {  
  31.                 return "";  
  32.             }  
  33.         }();  
  34.         this.Length = function ()  
  35.         {  
  36.             var ctrl = document.getElementById(ElementID);  
  37.             return ctrl.options.length;  
  38.         }();  
  39.         this.GetText = function (index)  
  40.         {  
  41.             var ctrl = document.getElementById(ElementID);  
  42.             return ctrl.options[index].text;  
  43.         };  
  44.         this.GetValue = function (index)  
  45.         {  
  46.             var ctrl = document.getElementById(ElementID);  
  47.             return ctrl.options[index].value;  
  48.         }  
  49.         this.AddItem = function (data)  
  50.         {  
  51.             var ctrl = document.getElementById(ElementID);  
  52.             var option = document.createElement("option");  
  53.             option.text = data;  
  54.             ctrl.add(option);  
  55.             return true;  
  56.         };  
  57.         this.AddValueItem = function (data, value)  
  58.         {  
  59.             var ctrl = document.getElementById(ElementID);  
  60.             var option = document.createElement("option");  
  61.             option.text = data;  
  62.             option.value = value;  
  63.             ctrl.add(option);  
  64.             return true;  
  65.         };  
  66.         this.RemoveItem = function (index)  
  67.         {  
  68.             var ctrl = document.getElementById(ElementID);  
  69.             ctrl.remove(index);  
  70.         };  
  71.         return this;  
  72.     }  
  73. }  
output

How to use above given method is shown below:
  1. Adding an Item to dropdown,
    1. arj.Select("dropdownid").AddItem(“Hello”);  
  2. Adding an item with value,
    1. arj.Select("dropdownid").AddValueItem(“Hello”, 1001);  
  3. Get value for given index position,
    1. arj.Select("dropdownid ").GetValue(1);  
  4. Get text for given index position,
    1. arj.Select("dropdownid").GetText(1);  

The attached code is fully-functional and ready to use with any existing project. Any feedback and suggestions are most welcome.