{"id":64433,"date":"2007-07-17T13:23:00","date_gmt":"2007-07-17T13:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/17\/how-can-i-display-a-default-message-if-nothing-has-been-typed-into-a-text-box\/"},"modified":"2007-07-17T13:23:00","modified_gmt":"2007-07-17T13:23:00","slug":"how-can-i-display-a-default-message-if-nothing-has-been-typed-into-a-text-box","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-display-a-default-message-if-nothing-has-been-typed-into-a-text-box\/","title":{"rendered":"How Can I Display a Default Message If Nothing Has Been Typed Into a Text Box?"},"content":{"rendered":"<p><P><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\"> <\/P>\n<P>Hey, Scripting Guy! Sometimes you go to a Web page and you\u2019ll see a text box that includes some instructions, something like \u201cPlease type a description in this box.\u201d When you click on the text box, those instructions disappear; however, if you click on the box, don\u2019t type in a description and then click somewhere else, then the instructions reappear. How can I do that same thing in an HTA?<BR><BR>&#8212; AJ <\/P>\n<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> <\/P>\n<P>Sorry; the Scripting Guy who writes this column is a little out of breath this morning; that\u2019s because he was in a race around 6:30 AM, and hasn\u2019t fully recovered. And in case you\u2019re wondering, that\u2019s a race he wasn\u2019t expecting to be in, a race he didn\u2019t realize he even <I>was<\/I> in, and a race in which he was soundly and decisively defeated.<\/P>\n<P>Sometime in the past month or so the Scripting Guys began starting off each day by eating a ceremonial doughnut. The ritual works like this. Each morning, on his way to work, the Scripting Guy who writes this column stops at the store and buys doughnuts. And each morning the remaining team members eat those doughnuts. Do any of these remaining team members ever buy doughnuts? Do any of these remaining team members at least help <I>pay<\/I> for these doughnuts? Let\u2019s put it this way: do any of these remaining team members ever do <I>anything<\/I>?<\/P>\n<P>\n<TABLE id=\"EHD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. Yes, this is <I>exactly<\/I> like the story of the <A href=\"http:\/\/pbskids.org\/\" target=\"_blank\"><B>Little Red Hen<\/B><\/A>. \u201cWho will help me buy these doughnuts?\u201d says the Little Red Hen who writes this column. No answer. \u201cWho will help me pay for these doughnuts?\u201d says the Little Red Hen who writes this column. \u201cNow, who will help me <I>eat<\/I> these doughnuts?\u201d says the Little Red Hen who writes this column.<\/P>\n<P>You can figure out the rest on your own.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/P>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, this morning the Scripting Guy who writes this column was grabbing a couple of doughnuts out of the doughnut case when he noticed an older woman taking a muffin out of the muffin case. (He noticed her because, for some reason, it\u2019s very unusual for him to <I>ever<\/I> see anyone who\u2019s older than he is. That hasn\u2019t always been the case, but over the past few years something seems to have changed.)<\/P>\n<P>Having grabbed his two doughnuts the Scripting Guy who writes this column began making his way towards the only open cash register. As he did so, he noticed that the muffin lady was scurrying to catch up to and then pass him. Unfortunately, she wasn\u2019t particularly fast, and the Scripting Guy who writes this column was able to keep pace with her, even though he was moseying along in his typical fashion. (In his defense, however, he <I>was<\/I> on his way to work; it goes without saying that he was in no hurry whatsoever.)<\/P>\n<P>As they approached the checkout stand, the lady took a desperate lunge and cut in front of the Scripting Guy who writes this column, a Scripting Guy who had to stop abruptly to make sure he didn\u2019t crash into the woman. The muffin lady paid for her purchase and left. The Scripting Guy who writes this column then did the same thing, calculating that the muffin lady had saved nearly 30 seconds by getting to the checkout stand ahead of him. <\/P>\n<P>And yes, that means that the Scripting Guy who writes this column could have saved 30 seconds of his own valuable time by forcing the issue and trying to beat the muffin lady to the checkout stand. But the Scripting Guy who writes this column lives such an exciting, fun-filled life that he decided he could afford to waste 30 seconds. And so he did.<\/P>\n<P>Of course, that doesn\u2019t mean that the Scripting Guy who writes this column can <I>always<\/I> afford to waste 30 seconds out of his life. After all, that might not leave time for his other daily rituals, like the one in which he tells people how to create a text box that displays a default message, causes that message to disappear when the text box gets the focus, then causes that message to <I>reappear<\/I> if the user doesn\u2019t end up entering a new value in the box.<\/P>\n<P>Not sure what that actually means? Then copy the following code, paste it into Notepad, save the resulting file as an HTML Application (<A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA<\/B><\/A>), and then give it a try for yourself:<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>Sub TextBoxEnter\n    If TextBox1.Value = &#8220;Please enter a value here.&#8221; Then\n        TextBox1.Value = &#8220;&#8221;\n    End If     \nEnd Sub<\/p>\n<p>Sub TextBoxLeave\n    If TextBox1.Value = &#8220;&#8221; Then\n        TextBox1.Value = &#8220;Please enter a value here.&#8221;\n    End If     \nEnd Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;TextBox1&#8243; size=&#8221;50&#8243; value=&#8221;Please enter a value here.&#8221; \n     onFocus = &#8220;TextBoxEnter&#8221; onFocusOut=&#8221;TextBoxLeave&#8221;&gt;&lt;p&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;TextBox2&#8243; size=&#8221;50&#8243;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Good idea: let\u2019s take a closer look at the code and see if we can figure out how this HTA works. To begin with, it\u2019s a very simple little HTA; for one thing, the body consists of just two controls (a pair of text boxes). One text box \u2013 which we named TextBox2 \u2013 is just a plain old text box; that\u2019s why the HTML tagging is so concise:<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;text&#8221; name=&#8221;TextBox2&#8243; size=&#8221;50&#8243;&gt;\n<\/PRE>\n<P>The other text box, which we named TextBox1 (and no, we didn\u2019t hire a consultant to help us; we came up with these names all by ourselves) is a little fancier:<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;text&#8221; name=&#8221;TextBox1&#8243; size=&#8221;50&#8243; value=&#8221;Please enter a value here.&#8221;\nonFocus = &#8220;TextBoxEnter&#8221; onFocusOut=&#8221;TextBoxLeave&#8221;&gt;\n<\/PRE>\n<P>So what <I>is<\/I> all that fancy stuff? For starters, we need to have a default value assigned to this text box; when you first open the HTA we want the text box to display the message <B>Please enter a value here.<\/B> That\u2019s why we added the <B>value<\/B> parameter, like so:<\/P><PRE class=\"codeSample\">value=&#8221;Please enter a value here.&#8221;\n<\/PRE>\n<P>In addition to having a default value, we also need this text box to respond to two different events: <B>onFocus<\/B>, an event triggered any time someone clicks onto or otherwise selects the text box; and <B>onFocusOut<\/B>, an event triggered any time someone clicks out of or otherwise moves the focus <I>away<\/I> from the text box. When either of those events occur we need to run the appropriate subroutine; each time the text box gets the focus we want to run a subroutine named TextBoxEnter, and each time the text box loses the focus we want to run a subroutine named TextBoxLeave.<\/P>\n<P>\n<TABLE id=\"EXF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. OK, for those subroutine names we <I>did<\/I> have to hire a high-priced consulting firm. But usually we come up with our own names for everything.<\/P>\n<P>Which explains how we ended up with family members and pets that have names like the Scripting Son and the Scripting Dog.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/P>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you might expect, all the fun stuff occurs inside those two subroutines. Let\u2019s start out by looking at the subroutine that runs any time the text box gains the focus:<\/P><PRE class=\"codeSample\">Sub TextBoxEnter\n    If TextBox1.Value = &#8220;Please enter a value here.&#8221; Then\n        TextBox1.Value = &#8220;&#8221;\n    End If     \nEnd Sub\n<\/PRE>\n<P>By default, our text box displays the message <I>Please enter a value here<\/I>. That\u2019s fine; after all, that\u2019s what we <I>want<\/I> the text box to display. However, we don\u2019t want to <I>always<\/I> display that message; if someone clicks inside the text box we\u2019d like that message to disappear. (Otherwise they\u2019d have to select the message, delete it, and <I>then<\/I> start typing in a value.) So how do we make the message disappear? That\u2019s easy; we simply set the value of the text box to an empty string:<\/P><PRE class=\"codeSample\">TextBox1.Value = &#8220;&#8221;\n<\/PRE>\n<P>Note, however, that we don\u2019t set the value of the text box to an empty string <I>any<\/I> time that control gets the focus; instead, we \u201creset\u201d the text box only if the message \u201cPlease enter a value here.\u201d happens to be displayed. Why do we do that? Well, suppose someone enters a value like the following into the text box:<\/P><PRE class=\"codeSample\">Pharmaceutical Research Division, Fabrikam Corp\n<\/PRE>\n<P>As you can see, the period was left off the end; the value typed into the box should have really been this:<\/P><PRE class=\"codeSample\">Pharmaceutical Research Division, Fabrikam Corp.\n<\/PRE>\n<P>But that\u2019s no big deal; after all, the user can just click at the end of the text box and then type in the missing period. Or, to be a little more precise, they can do that as long as we don\u2019t automatically delete the value of the text box each time the text box regains the focus. <I>That\u2019s<\/I> why we don\u2019t automatically reset the value of the text box; we do that only if the default value (\u201cPlease enter a value here.\u201d) is displayed. Otherwise any time someone clicked in the text box we\u2019d erase everything they\u2019d previously typed into the box. That might be OK. But, then again, it might not be.<\/P>\n<P>Now, what happens when the text box <I>loses<\/I> the focus? This is what happens:<\/P><PRE class=\"codeSample\">Sub TextBoxLeave\n    If TextBox1.Value = &#8220;&#8221; Then\n        TextBox1.Value = &#8220;Please enter a value here.&#8221;\n    End If     \nEnd Sub\n<\/PRE>\n<P>In effect, our onFocusOut subroutine is a mirror image of the onFocus subroutine. This time around, we check to see if the value of TextBox1 is equal to an empty string. If it is, that means that nothing at all has been typed into the text box. As a result, we set the value of the box to the default message: <I>Please enter a value here<\/I>. That\u2019s ensures that we never have a truly empty text box; if the box <I>is<\/I> empty we redisplay the default message. And what do we do if something other than the default value has been typed into the text box? Nothing; as long as the text box has <I>something<\/I> in it we simply leave the value alone. Combined, these two subroutines should provide the very behavior you were looking for.<\/P>\n<P>Now, to tell you the truth, the Scripting Guy who writes this column wasn\u2019t all that distraught about the muffin lady having beaten him to the checkout stand; in fact, he really didn\u2019t pay much attention to it. Apparently, however, the muffin lady <I>did<\/I> give it some thought; as the Scripting Guy who writes this column left the store the muffin lady was waiting for him. \u201cI\u2019m sorry I raced in front of you,\u201d she said. \u201cThat was very rude of me. I should have let you go ahead of me.\u201d<\/P>\n<P>She should have <I>let<\/I> him get ahead of her? You know, if you\u2019ve ever wondered how far the Scripting Guy who writes this column has fallen that should answer your question: little old ladies are now apologizing for being too fast and not <I>letting<\/I> him get ahead of them. <\/P>\n<P>Gosh, if little old ladies are passing him by that means that the <I>Scripting <\/I><I>Ed<\/I><I>itor<\/I> might soon be passing him by as well. Now there\u2019s a scary thought \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Sometimes you go to a Web page and you\u2019ll see a text box that includes some instructions, something like \u201cPlease type a description in this box.\u201d When you click on the text box, those instructions disappear; however, if you click on the box, don\u2019t type in a description and then click somewhere [&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-64433","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! Sometimes you go to a Web page and you\u2019ll see a text box that includes some instructions, something like \u201cPlease type a description in this box.\u201d When you click on the text box, those instructions disappear; however, if you click on the box, don\u2019t type in a description and then click somewhere [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64433","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=64433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64433\/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=64433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}