Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting the text item in a dropdown list

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
How do I programmatically select a text item (not the value) of a html dropdown list?

I know this works for the value:

document.Combo1.value = "Breaker"

but I have a dropdown list that looks like this:

Code:
<option value="Breaker">100-2100</option>
<option value="Compressor">100-2155</option>
<option value="Drill">100-2240</option>
<option value="Drill">100-2242</option>

and I want to select the code (100-2100) in the list instead of the value ("Breaker").

Thanks.

 
Hello dpdoug,

Suppose their parent & ancestor are like this.
[tt]
<form name="requisition">
<select name="partnumber">
<option value="Breaker">100-2100</option>
<option value="Compressor">100-2155</option>
<option value="Drill">100-2240</option>
<option value="Drill">100-2242</option>
</select>
</form>
[/tt]
Then, option 100-2100 can be chosen by setting the selected property to true. (Similarly for other indices.)
[tt]
document.forms("requisition").partnumber.options(0).selected=true
[/tt]
regards - tsuji
 
But suppose that I want to select the option 100-2242, but I don't know that it's index is 3. How do I select it by the text?

Like so?:

Code:
document.forms("requisition").partnumber.options("100-2242").selected=true

by the way, thanks for taking the time to answer this.
 
dpdoug,

In that case, maybe just assigning an id to the options.
[tt]
<option id="100-2100" value="Breaker">100-2100</option>
<option id="100-2155" value="Compressor">100-2155</option>
<option id="100-2240" value="Drill">100-2240</option>
<option id="100-2242" value="Drill">100-2242</option>
[/tt]
Then, use getElementById to view as those tags in flat structure:
[tt]
document.getElementById("100-2242").selected=true
[/tt]
Mind conflict in id in designing it.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top