Wednesday, November 28, 2007

Dev : Getting HTML Dropdown/Combobox Label, Not Value

Something quite interesting on combobox my colleague had just asked me. Using javascript, how do we get the label value, instead of the real value of a combobox. well, umm... har.... how har? :-) I am not an expert in HTML and javascript, so scratching my head is the only thing I can do.

But after some research on the Net, I've found some resources that helped to solve the problem. You don't need to hide the label value somewhere or in the html option tag, just create your dropdown/combobox as normal, using this three tiny line of code in your javascript, and you're done!

var comboList = document.myform.combolist;
var comboValue = comboList.value;
var comboLabel = comboList.options[comboList.options.selectedIndex].text;