{"id":63893,"date":"2007-10-02T05:12:00","date_gmt":"2007-10-02T05:12:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/02\/hey-scripting-guy-how-can-i-automatically-move-the-focus-as-users-type-in-an-hta\/"},"modified":"2007-10-02T05:12:00","modified_gmt":"2007-10-02T05:12:00","slug":"hey-scripting-guy-how-can-i-automatically-move-the-focus-as-users-type-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-automatically-move-the-focus-as-users-type-in-an-hta\/","title":{"rendered":"Hey, Scripting Guy! How Can I Automatically Move the Focus as Users Type in an HTA?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>I have an HTA that we use for creating and modifying user accounts. In that HTA we have a couple of phone number fields; each of these fields consists of three text boxes (one for the area code and two for the phone number). I\u2019d like to set this up so that, as soon as someone has typed three digits into the area code box the cursor automatically jumps to the first phone number box; as soon as someone types three digits in <I>that<\/I> text box I\u2019d like the cursor to automatically jump to the third and final text box. How can I do that?<BR><BR>&#8212; TJ<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, TJ. Wow. Gee, that\u2019s a tough one, TJ and, to be honest, we don\u2019t see how we can answer that; in fact, we aren\u2019t even sure that there <I>is<\/I> an answer to that question. To tell you the truth, we feel really bad about that, and we feel even worse that we won\u2019t be able to answer anyone\u2019s question today. But that\u2019s just the way it goes; we\u2019ll try again tomorrow. Sorry.<\/P>\n<P>And no, this has <I>nothing<\/I> to do with the Scripting Guy who writes this column intending to sneak out early in order to catch today\u2019s tiebreaker game between the San Diego Padres and the Colorado Rockies. You say that San Diego and Colorado are playing today, with the winner moving on and the loser going home? We had no idea; in fact, we thought the baseball season ended a long time ago. How about that? We &#8212;<\/P>\n<P>OK, so you caught us: we <I>are <\/I>planning on sneaking out early so that we can go home and watch the baseball game. And so we\u2019ll make a deal with you TJ: we\u2019ll show you how to automatically move the focus in an HTA and, in return, if our manager calls looking for us you tell him we\u2019re in a meeting, a meeting that will last the rest of the day. Deal? Deal.<\/P>\n<P>OK, here\u2019s our end of the bargain:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub Window_Onload\n        TextBox1.Focus\n    End Sub<\/p>\n<p>    Sub TextBoxCheck\n        If Len(TextBox1.Value) = 3 Then\n            TextBox2.Focus\n            TextBox2.Select\n        End If\n    End Sub<\/p>\n<p>    Sub TextBoxCheck2\n        If Len(TextBox2.Value) = 3 Then\n            TextBox3.Focus\n            TextBox3.Select\n        End If\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;TextBox1&#8243; size=&#8221;10&#8243; maxLength=&#8221;3&#8243; onKeyUp=&#8221;TextBoxCheck&#8221;&gt;-\n    &lt;input type=&#8221;text&#8221; name=&#8221;TextBox2&#8243; size=&#8221;10&#8243; maxLength=&#8221;3&#8243; onKeyUp=&#8221;TextBoxCheck2&#8243;&gt;-\n    &lt;input type=&#8221;text&#8221; name=&#8221;TextBox3&#8243; size=&#8221;10&#8243; maxLength=&#8221;4&#8243;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Let\u2019s see if we can figure out how this baby works. (And let\u2019s see if we can do so in 15 minutes or less.) To begin with, you might have noticed that we have three text boxes in the body of our HTA. (Just to add that extra special little touch, we\u2019ve also put hyphens between each box, making the whole thing look a little more like a phone number.) Let\u2019s take a look at the HTML tagging for text box No. 1:<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;text&#8221; name=&#8221;TextBox1&#8243; size=&#8221;10&#8243; maxLength=&#8221;3&#8243; onKeyUp=&#8221;TextBoxCheck&#8221;&gt;\n<\/PRE>\n<P>As you can see, this is nothing more than your basic &lt;INPUT&gt; tag, along with the following five parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>type=\u201dtext\u201d<\/B>. This is just standard HTML to indicate that we want to use a text box control and not, say, a button or checkbox.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>name=\u201dTextBox1\u201d<\/B>. This is the name given to our text box; each text box has to have a unique name so that we can refer to the individual control boxes. (And, yes, now that you mention it, the Scripting Guys did come up with the name \u201cTextBox1\u201d all by ourselves. How did you know?)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>size=\u201d10\u201d<\/B>. The size property simply specifies the width of the text box, in characters. We picked 10 because \u2026 well, just because.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>maxLength=\u201d3\u201d<\/B>. This is a very useful property to know about: maxLength specifies the maximum number of characters that can be typed into the text box. Because a U.S. area code never has more than 3 digits, we set the maxLength of the text box to 3. Once you\u2019ve entered 3 characters into the text box you can pound the keyboard all you want; you\u2019ll never be able to wedge a fourth or fifth character into the box. Never.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>onKeyUp=\u201dTextBoxCheck\u201d<\/B>. The onKeyUp event is fired any time the focus is in the text box and someone presses and releases a key on the keyboard. The very moment that key is released the onKeyUp event is fired and our HTA immediately runs the subroutine TextBoxCheck (which we\u2019ll discuss in just a second).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>And then we have similar HTML tagging for our other two text boxes. But it <I>is<\/I> getting late, so we\u2019ll let you figure out that part for yourself. (It\u2019ll give you something to do while we\u2019re at our, uh \u2026 meeting.)<\/P>\n<P>In addition to the three text boxes, our HTA also has three subroutines. The first subroutine, <B>Window_OnLoad<\/B>, is automatically called any time the HTA is loaded or refreshed. As you can see, this subroutine does only one thing: it places the focus (the cursor) in TextBox1. Why did we do that? That\u2019s easy: for this very simple HTA, that ensures that each time you run the application the focus will be in the first text box. In turn, that means you can start the HTA and immediately begin typing the area code.<\/P>\n<P>This isn\u2019t something that you <I>have<\/I> to do; we just thought it was another one of those nice little touches.<\/P>\n<P>Our next two subroutines (TextBoxCheck and TextBoxCheck2, two more names we Scripting Guys came up with ourselves) represent the heart and soul of our little HTA. Let\u2019s take a peek at the TextBoxCheck subroutine which, as you recall, is triggered any time a user types something in the text box set aside for the area code:<\/P><PRE class=\"codeSample\">Sub TextBoxCheck\n    If Len(TextBox1.Value) = 3 Then\n        TextBox2.Focus\n        TextBox2.Select\n    End If\nEnd Sub\n<\/PRE>\n<P>So what\u2019s going on here? Well, each time someone types something in the text box we use the VBScript <B>Len<\/B> function to see how many characters, in total, have been typed into the box. (That is, how many characters are in the text box\u2019s <B>Value<\/B>.) Suppose the length (number of characters) is 3. As it turns out, that\u2019s the maximum number of characters allowed in the text box (and, not coincidentally, in an area code). Therefore we do two things:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>First, we move the focus from TextBoxt1 to TextBox2. That\u2019s what the code <B>TextBox2.Focus<\/B> is for.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Second, we select any existing text in TextBox2. We don\u2019t have to do that, but that allows you to start typing and automatically replace any existing text in TextBox2. If you don\u2019t like that feature then simply remove the line <B>TextBox2.Select<\/B>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>See how that works? You type the first character in the text box and, the moment you release the key, the subroutine is executed. Inside that subroutine we check to see how many characters have been typed in the text box. So far we have just 1 character in there, so nothing happens. Type a second character in the text box and, again, nothing happens. (Why not? Because nothing is <I>supposed<\/I> to happen, not unless there are 3 characters in the box.) Type a <I>third<\/I> character, however, and upon releasing the key, the focus will immediately jump to TextBox2. <\/P>\n<P>Which is just exactly what we <I>wanted<\/I> it to do.<\/P>\n<TABLE id=\"EAG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. As you probably guessed, the subroutine TextBoxCheck2 is very similar. The only differences? For one, this subroutine is fired each time something is typed into TextBox2; for another, when 3 characters have been entered into the text box the focus jumps to TextBox3.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That should do it, TJ. You could, of course, add in even more elaborate checking, to ensure that only numbers are typed into the boxes, but that\u2019s an exercise we\u2019ll save for another day. <\/P>\n<P>Uh, never mind. As it turns out, the Scripting Editor doesn\u2019t like us to say that we\u2019ll save something for another day. \u201cYou say that all time,\u201d she huffed, \u201cand then half the time you never followed through.\u201d Just to make her happy (needless to say, our sole purpose in life is to make the Scripting Editor happy) we traveled back in time and wrote a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jul07\/hey0711.mspx\"><B>Hey, Scripting Guy! column<\/B><\/A> that shows one way you can restrict the characters that can be typed into a text box.<\/P>\n<P>And then, just for the heck of it, we traveled <I>forward<\/I> in time in order to see who\u2019s going to win this year\u2019s World Series. If you\u2019re a Boston fan, well, you might want to limit the amount you bet on your beloved Red Sox. Just a little money-saving advice from the Scripting Guys.<\/P>\n<P>Oh, and in case you\u2019re wondering, the Scripting Guy who writes this column is predicting that the Cleveland Indians will win this year\u2019s World Series. And you\u2019re right: Cleveland is definitely <I>not<\/I> the favorite, and the Scripting Guy who writes this column is risking public ridicule by making such a prediction. But that\u2019s OK; after all, if he\u2019s wrong he\u2019ll just travel back in time and rewrite his prediction, inserting the name of the team that really <I>will<\/I> win the World Series this year.<\/P>\n<P>But there\u2019s no time for time-travel now; instead, the Scripting Guy who writes this column is off to a meeting. (Remember, a deal\u2019s a deal.) See you tomorrow.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have an HTA that we use for creating and modifying user accounts. In that HTA we have a couple of phone number fields; each of these fields consists of three text boxes (one for the area code and two for the phone number). I\u2019d like to set this up so that, as soon as [&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-63893","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>I have an HTA that we use for creating and modifying user accounts. In that HTA we have a couple of phone number fields; each of these fields consists of three text boxes (one for the area code and two for the phone number). I\u2019d like to set this up so that, as soon as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63893","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=63893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63893\/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=63893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}