I have several places on my website where combobox style management is required. To do this, I encapsulated the combobox control in a server-side .NET component for reuse. Some pages may have a combo box, and some may not.
In the kernel itself, this combo box contains a text box and a drop-down menu aligned appropriately with CSS. Part of the processed HTML is simple:
<select> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> <option>Option 4</option> </select>
The popup menu should start from scratch, and changing any value (including "Option 1") should trigger an onchange event.
I accomplish this by doing something like:
document.getElementById("theSelectElement").selectedIndex = -1;
but I would prefer not to run javascript for every element on the page. I understand that I could use jQuery to select against the CSS class, but most pages on it will not have a select element and nothing will happen.
Is there any way to set selectedIndex enclosed in the tag itself? Sort of:
<select selectedIndex="-1">...
or
<select onload="this.selectedIndex = -1">...
?
source share