{"id":64343,"date":"2007-07-31T01:04:00","date_gmt":"2007-07-31T01:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/31\/how-can-i-dynamically-link-two-drop-down-lists\/"},"modified":"2007-07-31T01:04:00","modified_gmt":"2007-07-31T01:04:00","slug":"how-can-i-dynamically-link-two-drop-down-lists","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-dynamically-link-two-drop-down-lists\/","title":{"rendered":"How Can I Dynamically Link Two Drop-Down Lists?"},"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! How can I configure a pair of dropdown lists in an HTA so that, when I change the selection in the first list, the options available in the second list also change?<BR><BR>&#8212; PC <\/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, PC. Before we get going today, we\u2019d like to make it clear that the Scripting Guy who writes this column isn\u2019t the least bit tired. Sure, he and the Scripting Son went to the midnight showing of <I>The Simpsons Movie <\/I>last night, which meant that the Scripting Guy who writes this column didn\u2019t get to bed until 2:15 AM, then had to get up less than four hours later in order to get to work and write this column. But why would that make him tired? Sure, staying up all night might make some <I>old<\/I> guy tired. But the Scripting Guy who writes this column? He\u2019s not the least bit \u2026 um, what was that word again? Right. Tired. He\u2019s not the least bit tired.<\/P>\n<P>Not the bit least.<\/P>\n<P>Um, not the least bit.<\/P>\n<P>At any rate, seeing as how the Scripting Guy who writes this column is bright-eyed and bushy-tailed, and seeing as how he <I>did<\/I> remember to put on pants this morning (hold on a second \u2026 yes, he did), let\u2019s see what we can do to address PC\u2019s question. Which was \u2026 it\u2019s on the tip of our tongue \u2026 oh, right. PC has an HTML Application (more familiarly known as an HTA) that features a pair of drop-down lists. When he makes a selection in the first list, he\u2019d like the options in the second list to dynamically change in response to that choice. How can he do that? We have absolutely no idea. Good night; see you in the morning.<\/P>\n<P>Wait a second; don\u2019t go. It turns out that we have at least <I>one<\/I> idea as to how he can do this:<\/P><PRE class=\"codeSample\">&lt;Script Language=&#8221;VBScript&#8221;&gt;\n    Sub RunScript\n        For Each objOption in DynamicOptions.Options\n            objOption.RemoveNode\n        Next <\/p>\n<p>        If PickCategory.Value = &#8220;Characters&#8221; Then\n            arrValues = Array(&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;, &#8220;E&#8221;)\n        End If<\/p>\n<p>        If PickCategory.Value = &#8220;Numbers&#8221; Then\n            arrValues = Array(&#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221;, &#8220;5&#8221;)\n        End If<\/p>\n<p>        For Each strValue in arrValues\n            Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n            objOption.Text = strValue\n            objOption.Value = strValue  \n            DynamicOptions.Add(objOption)\n        Next     \n    End Sub\n&lt;\/Script&gt;<\/p>\n<p>&lt;select size=&#8221;1&#8243; name=&#8221;PickCategory&#8221; style=&#8221;width:100px&#8221; onChange=&#8221;RunScript&#8221;&gt;\n    &lt;option value=&#8221;Characters&#8221;&gt;Characters&lt;\/option&gt;\n    &lt;option value=&#8221;Numbers&#8221;&gt;Numbers&lt;\/option&gt;\n&lt;\/select&gt;<\/p>\n<p>&lt;P&gt;<\/p>\n<p>&lt;select size=&#8221;1&#8243; name=&#8221;DynamicOptions&#8221; style=&#8221;width:100px&#8221;&gt;\n    &lt;option value=&#8221;A&#8221;&gt;A&lt;\/option&gt;\n    &lt;option value=&#8221;B&#8221;&gt;B&lt;\/option&gt;\n    &lt;option value=&#8221;C&#8221;&gt;C&lt;\/option&gt;\n    &lt;option value=&#8221;D&#8221;&gt;D&lt;\/option&gt;\n    &lt;option value=&#8221;E&#8221;&gt;E&lt;\/option&gt;\n&lt;\/select&gt;\n<\/PRE>\n<P>As you can see, there\u2019s really not too much to this particular HTA. (If you aren\u2019t sure what an HTA even <I>is<\/I> then you might want to take a peek at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>.) For example, the body of our HTA contains only a pair of drop-down lists. The first drop-down list is named PickCategory and includes just two options:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Characters (the default value)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Numbers<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>The premise here is simple: the user is going to select one of these two options. If the user selects <B>Characters<\/B> then, on-the-fly, we\u2019ll display a series of letters as the individual options available in our second drop-down list (which we christened DynamicOptions). If the user selects <B>Numbers<\/B>, we\u2019ll display a series of numbers as the options available in the second drop-down list. Each time we change the value of PickCategory we\u2019ll run a subroutine named RunScript; hence the parameter <B>onChange=&#8221;RunScript&#8221;<\/B>: <\/P><PRE class=\"codeSample\">&lt;select size=&#8221;1&#8243; name=&#8221;PickCategory&#8221; style=&#8221;width:100px&#8221; onChange=&#8221;RunScript&#8221;&gt;\n    &lt;option value=&#8221;Characters&#8221;&gt;Characters&lt;\/option&gt;\n    &lt;option value=&#8221;Numbers&#8221;&gt;Numbers&lt;\/option&gt;\n&lt;\/select&gt;\n<\/PRE>\n<P>That second drop-down list, as we noted, is named DynamicOptions; when the HTA opens it displays, by default, a series of characters as the drop-down options (if you aren\u2019t familiar with the HTML tagging for creating a drop-down list then take a look at one of our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/htas\/tutorial2.mspx\"><B>HTA tutorials<\/B><\/A>). The complete tagging for this drop-down list looks like this:<\/P><PRE class=\"codeSample\">&lt;select size=&#8221;1&#8243; name=&#8221;DynamicOptions&#8221; style=&#8221;width:100px&#8221;&gt;\n    &lt;option value=&#8221;A&#8221;&gt;A&lt;\/option&gt;\n    &lt;option value=&#8221;B&#8221;&gt;B&lt;\/option&gt;\n    &lt;option value=&#8221;C&#8221;&gt;C&lt;\/option&gt;\n    &lt;option value=&#8221;D&#8221;&gt;D&lt;\/option&gt;\n    &lt;option value=&#8221;E&#8221;&gt;E&lt;\/option&gt;\n&lt;\/select&gt;\n<\/PRE>\n<P>Of course, these default options aren\u2019t set in stone; instead, they\u2019re going to change each time the user makes a new selection in the PickCategory drop-down. How? Don\u2019t worry; we\u2019ll tell you how. <\/P>\n<P>Zzzzz \u2026 <\/P>\n<P>Sorry. Just resting our eyes.<\/P>\n<P>Now, where were we? That\u2019s odd; we have absolutely no idea what we were just talking about. But that\u2019s all right; let\u2019s just go ahead and talk about the subroutine RunScript, the subroutine that runs any time the user makes a new selection in the PickCategory drop-down list.<\/P>\n<P>Zzzzz \u2026 <\/P>\n<P>Um, so what <I>does<\/I> happen in the RunScript subroutine, you ask? To begin with, we use this block of code to remove all the existing options from the DynamicOptions drop-down:<\/P><PRE class=\"codeSample\">For Each objOption in DynamicOptions.Options\n    objOption.RemoveNode\nNext\n<\/PRE>\n<P>Why bother with this? That\u2019s easy: each time we make a change in the first drop-down list we want to completely replace all the options shown in the second list. The easiest way to do that is simply to remove all the existing options and then start adding new options from scratch.<\/P>\n<P>Speaking of which, our next step is to determine the actual value that was selected in drop-down list 1, then begin the process of assigning the appropriate set of options to drop-down list 2. (To that end, the first thing we\u2019ll do is put all those options into an array named arrValues). The following block of code determines whether or not the value <I>Characters<\/I> was selected in our first drop-down; if so, it then assigns a series of letters to arrValues:<\/P><PRE class=\"codeSample\">If PickCategory.Value = &#8220;Characters&#8221; Then\n    arrValues = Array(&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;, &#8220;E&#8221;)\nEnd If\n<\/PRE>\n<P>Meanwhile, this block of code assigns a series of numbers to arrValues, but only if <I>Numbers<\/I> was selected in drop-down 1:<\/P><PRE class=\"codeSample\">If PickCategory.Value = &#8220;Numbers&#8221; Then\n    arrValues = Array(&#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221;, &#8220;5&#8221;)\nEnd If\n<\/PRE>\n<P>Once we\u2019ve assigned the appropriate set of values to arrValues we then use this block of code to create the new options for drop-down list 2:<\/P><PRE class=\"codeSample\">For Each strValue in arrValues\n    Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n    objOption.Text = strValue\n    objOption.Value = strValue  \n    DynamicOptions.Add(objOption)\nNext\n<\/PRE>\n<P>What we\u2019ve done here is set up a For Each loop to loop through all the items in the array arrValues. For each of those items we create an instance of the <B>Option<\/B> object; this object represents an individual option in a drop-down list. For each instance of the Option object we then assign the value of the array item to both the <B>Text<\/B> and <B>Value<\/B> properties:<\/P><PRE class=\"codeSample\">objOption.Text = strValue\nobjOption.Value = strValue\n<\/PRE>\n<P>In other words, if the first value in our array is <I>A<\/I> we create a drop-down list option that not only has the label (Text) <B>A<\/B> but has the same value as well. We then use the <B>Add<\/B> method to add this new option to drop-down list 2:<\/P><PRE class=\"codeSample\">DynamicOptions.Add(objOption)\n<\/PRE>\n<P>And then we loop around and repeat this process until all the items in the array arrValues have been configured as drop-down list options. Once that\u2019s finished we are done.<\/P>\n<P>And not a moment too soon. Do you realize that, as we write this column, it\u2019s not even 10:00 AM yet? This is going to be a long day.<\/P>\n<P>Incidentally, for those of you who haven\u2019t seen <I>The Simpsons Movie<\/I> yet, the Scripting Guy who writes this column found it refreshingly-good. There\u2019s no doubt that the Simpsons glory days are behind them (no; why <I>would<\/I> a statement like that make anyone think of the Scripting Guys?); nevertheless, the movie had some pretty entertaining parts to it. For example, the Scripting Guy who writes this column has spent most of this morning singing the Spider-Pig song:<\/P>\n<P><I>Spider-Pig, Spider-Pig<\/I><BR><I>Does whatever a Spider-Pig does.<\/I><BR><I>Can he swing from a web?<\/I><BR><I>No, he can\u2019t; he\u2019s a pig.<\/I><BR><I>Look out: here comes the Spider-Pig.<\/I><\/P>\n<P>Trust us: that\u2019s a good song. <\/P>\n<P>Or at least it is at 2:00 in the morning.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I configure a pair of dropdown lists in an HTA so that, when I change the selection in the first list, the options available in the second list also change?&#8212; PC Hey, PC. Before we get going today, we\u2019d like to make it clear that the Scripting Guy who writes [&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-64343","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! How can I configure a pair of dropdown lists in an HTA so that, when I change the selection in the first list, the options available in the second list also change?&#8212; PC Hey, PC. Before we get going today, we\u2019d like to make it clear that the Scripting Guy who writes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64343","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=64343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64343\/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=64343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}