PDA

View Full Version : How to retrieve the index of a select box using value


htmlcssnewbie
03-10-2009, 05:45 PM
I have a select box. I have a certain value (as opposed to text). I wish to find out what index maps to this value (and, by proxy, text).

I used to remember how to do this. I can loop through the select box and find out the index in that fashion. There must be another way. Any help would be appreciated.

Thank you!

rangana
03-11-2009, 03:49 AM
I'm not sure what you're trying to relay, but have you tried:

<select id="myid" onchange="alert('The index is: '+this.options[this.options.selectedIndex].index);">
<option value="test1">Val1</option>
<option value="test2">Val2</option>
<option value="test3">Val3</option>
<option value="test4">Val4</option>
<option value="test5">Val5</option>
</select>

htmlcssnewbie
03-11-2009, 09:08 AM
Yeah, I'm aware of that but that's not the problem. The problem is I'm pre-selecting an option based on its value and I do not know what its index is. I know the value I want, but I want to select it, and I need to know the index of the value to select it. The question is how do I find the index based on its value?

I can use a for loop but I thought there was another, simpler way.

Thanks.

rangana
03-11-2009, 08:56 PM
Could you please show the code on how yo pre-selected the options.