Hey, Scripting Guy! In my HTA, how can I get the actual text of a list box or dropdown list option?
— MM
Hey, MM. You might be familiar with an old Mac Davis song, a song that starts off like this:
Oh Lord it’s hard to be humble When you’re perfect in every way
Now, admittedly, the Scripting Guys aren’t perfect in every way. But – all modesty aside – there is one area where we have an absolutely perfect record: our initial response to any Hey, Scripting Guy! inquiry is always wrong.
Your question is a, well, perfect example of that. When you add a dropdown list to an HTA (HTML Application), you use code similar to this:
<select size=”1″ name=”OptionChooser” onChange=”TestSub”> <option value=”0″></option> <option value=”1″>Option 1</option> <option value=”2″>Option 2</option> <option value=”3″>Option 3</option> </select>
As you can see, we our dropdown list includes several options (the actual items that appear in the list), and each option has a unique value. Take the second option, for example. The text that actually appears in the dropdown list is Option 1, but the value (the item typically used by a script) is 1. The value and the text don’t have to be different, but they usually are, and often-times they need to be. For example, in a dropdown list of web sites you might want your text to read Script Center even though the value needs to be the Script Center URL. Thus the value and the text end up different.
Note. If you have no idea what we’re talking about, HTAs provide a way to give your scripts a graphical user interface. If that intrigues you (and it should; HTAs can be very cool) you might want to take a look at the HTA Developers Center and, in particular, our tutorial on creating HTAs. |
Generally speaking, the fact that the value and the text are different doesn’t matter much…unless, of course, you need to get at the text of an individual option. Can you do that? Our first reaction was this: no way; not in a million years.
Well, if nothing else at least we preserved our perfect record: as it turns out there is a way to get at the text of an individual option. On top of that, you don’t even have to do anything special to the dropdown list; instead, all you need to do is access the InnerText property of the selected option. Here’s a little subroutine that can retrieve the InnerText property for the option selected in a dropdown list (a list we named OptionChooser):
Sub TestSub For Each objOption in OptionChooser.Options If objOption.Selected Then Msgbox objOption.InnerText End If Next End Sub
To get at the value we set up a For Each loop that loops through the Options collection for the dropdown list. (As you probably guessed, the Options collection consists of all the options included in the dropdown list.) For each individual option we check to see if the Selected property is True; that’s what this line of code does:
If objOption.Selected Then
If Selected is True, that means this is the selected option. We then use this line of code to display the value of the InnerText property in a message box:
Msgbox objOption.InnerText
If Selected is False we just loop back around and check the next option in the collection.
And yes, it’s that easy, even if our first reaction was to say “Can’t be done.” If you’d like to try this out for yourself, here’s some HTA code that demonstrates the technique. Copy the code, paste it into Notepad, and then save the file with a .hta file extension:
<html> <head> <title>HTA Test</title> <HTA:APPLICATION ID=”objTest” APPLICATIONNAME=”HTATest” SCROLL=”no” SINGLEINSTANCE=”yes” > </head><SCRIPT LANGUAGE=”VBScript”>
Sub TestSub For Each objOption in OptionChooser.Options If objOption.Selected Then Msgbox objOption.InnerText End If Next End Sub
</SCRIPT>
<body>
<select size=”1″ name=”OptionChooser” onChange=”TestSub”> <option value=”0″></option> <option value=”1″>Option 1</option> <option value=”2″>Option 2</option> <option value=”3″>Option 3</option> </select>
</body>
You know, it is hard to be humble when you’re perfect. For some reason, though, the Scripting Guys have no trouble whatsoever in being humble….
0 comments