{"id":69523,"date":"2005-06-24T15:30:00","date_gmt":"2005-06-24T15:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/24\/how-can-i-get-the-actual-text-of-a-list-box-or-dropdown-list-option\/"},"modified":"2005-06-24T15:30:00","modified_gmt":"2005-06-24T15:30:00","slug":"how-can-i-get-the-actual-text-of-a-list-box-or-dropdown-list-option","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-the-actual-text-of-a-list-box-or-dropdown-list-option\/","title":{"rendered":"How Can I Get the Actual Text of a List Box or Dropdown List Option?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! In my HTA, how can I get the actual text of a list box or dropdown list option?<BR><BR>&#8212; MM<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MM. You might be familiar with an old Mac Davis song, a song that starts off like this:<\/P><PRE class=\"codeSample\">Oh Lord it&#8217;s hard to be humble\nWhen you&#8217;re perfect in every way\n<\/PRE>\n<P>Now, admittedly, the Scripting Guys aren\u2019t perfect in <I>every<\/I> way. But &#8211; all modesty aside &#8211; there is one area where we have an absolutely perfect record: our initial response to any <I>Hey, Scripting Guy!<\/I> inquiry is <I>always<\/I> wrong.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">&lt;select size=&#8221;1&#8243; name=&#8221;OptionChooser&#8221; onChange=&#8221;TestSub&#8221;&gt;\n    &lt;option value=&#8221;0&#8243;&gt;&lt;\/option&gt;\n    &lt;option value=&#8221;1&#8243;&gt;Option 1&lt;\/option&gt;\n    &lt;option value=&#8221;2&#8243;&gt;Option 2&lt;\/option&gt;\n    &lt;option value=&#8221;3&#8243;&gt;Option 3&lt;\/option&gt;\n&lt;\/select&gt;\n<\/PRE>\n<P>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 <B>Option 1<\/B>, but the <I>value<\/I> (the item typically used by a script) is <B>1<\/B>. The value and the text don\u2019t 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 <B>Script Center<\/B> even though the value needs to be the Script Center URL. Thus the value and the text end up different.<\/P>\n<TABLE class=\"dataTable\" id=\"EUD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. If you have no idea what we\u2019re 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A> and, in particular, our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/htas\/tutorial1.mspx\"><B>tutorial<\/B><\/A> on creating HTAs.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Generally speaking, the fact that the value and the text are different doesn\u2019t matter much\u2026unless, 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.<\/P>\n<P>Well, if nothing else at least we preserved our perfect record: as it turns out there <I>is<\/I> a way to get at the text of an individual option. On top of that, you don\u2019t even have to do anything special to the dropdown list; instead, all you need to do is access the <B>InnerText<\/B> property of the selected option. Here\u2019s a little subroutine that can retrieve the InnerText property for the option selected in a dropdown list (a list we named OptionChooser):<\/P><PRE class=\"codeSample\">Sub TestSub\n    For Each objOption in OptionChooser.Options\n        If objOption.Selected Then\n            Msgbox objOption.InnerText\n        End If\n    Next\nEnd Sub\n<\/PRE>\n<P>To get at the value we set up a For Each loop that loops through the <B>Options <\/B>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 <B>Selected<\/B> property is True; that\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">If objOption.Selected Then\n<\/PRE>\n<P>If Selected <I>is<\/I> 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:<\/P><PRE class=\"codeSample\">Msgbox objOption.InnerText\n<\/PRE>\n<P>If Selected is False we just loop back around and check the next option in the collection.<\/P>\n<P>And yes, it\u2019s that easy, even if our first reaction was to say \u201cCan\u2019t be done.\u201d If you\u2019d like to try this out for yourself, here\u2019s some HTA code that demonstrates the technique. Copy the code, paste it into Notepad, and then save the file with a .hta file extension:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=&#8221;objTest&#8221; \n     APPLICATIONNAME=&#8221;HTATest&#8221;\n     SCROLL=&#8221;no&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub TestSub\n        For Each objOption in OptionChooser.Options\n            If objOption.Selected Then\n                Msgbox objOption.InnerText\n            End If\n        Next\n    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;select size=&#8221;1&#8243; name=&#8221;OptionChooser&#8221; onChange=&#8221;TestSub&#8221;&gt;\n    &lt;option value=&#8221;0&#8243;&gt;&lt;\/option&gt;\n    &lt;option value=&#8221;1&#8243;&gt;Option 1&lt;\/option&gt;\n    &lt;option value=&#8221;2&#8243;&gt;Option 2&lt;\/option&gt;\n    &lt;option value=&#8221;3&#8243;&gt;Option 3&lt;\/option&gt;\n&lt;\/select&gt;<\/p>\n<p>&lt;\/body&gt;\n<\/PRE>\n<P>You know, it <I>is<\/I> hard to be humble when you\u2019re perfect. For some reason, though, the Scripting Guys have no trouble whatsoever in being humble\u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In my HTA, how can I get the actual text of a list box or dropdown list option?&#8212; MM Hey, MM. You might be familiar with an old Mac Davis song, a song that starts off like this:Oh Lord it&#8217;s hard to be humble When you&#8217;re perfect in every way Now, admittedly, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-69523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In my HTA, how can I get the actual text of a list box or dropdown list option?&#8212; MM Hey, MM. You might be familiar with an old Mac Davis song, a song that starts off like this:Oh Lord it&#8217;s hard to be humble When you&#8217;re perfect in every way Now, admittedly, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}