{"id":63403,"date":"2007-12-13T01:15:00","date_gmt":"2007-12-13T01:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/12\/13\/hey-scripting-guy-how-can-i-change-the-background-color-of-a-text-box\/"},"modified":"2007-12-13T01:15:00","modified_gmt":"2007-12-13T01:15:00","slug":"hey-scripting-guy-how-can-i-change-the-background-color-of-a-text-box","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-background-color-of-a-text-box\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Background Color of a Text Box?"},"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>Hey, Scripting Guy! I have an HTA that we use for data entry. On this form, some of the fields are mandatory and some of them are optional. I\u2019d like to indicate the mandatory fields by making those text boxes a different color than the text boxes used for the optional fields. How do I do that?<BR><BR>&#8212; IU<\/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, IU. You know, for a large number of people around the world the holiday season is fast approaching; that means that many of these people (the Scripting Guys included) are even-now desperately searching for those last-minute gifts. While the Scripting Guys don\u2019t endorse any products (although we would, and happily; it\u2019s just that no one\u2019s ever asked) we <I>do<\/I> suggest you check out the <A href=\"http:\/\/www.grandeportal.com\/Default.aspx?SecId=2&amp;CatId=0&amp;PrdId=2089&amp;PagId=10&amp;Key=&amp;OrdId=3\" target=\"_blank\"><B>SnapJack Pancake Cutter<\/B><\/A>. If you\u2019re like the Scripting Guys you typically don\u2019t eat pancakes for breakfast. Why not? Because pancakes are just too darned hard to cut! (Of course, that <I>could<\/I> have something to do with the recipe we\u2019ve been using. Is it flour <I>and<\/I> milk? Because we\u2019ve been using flour <I>or<\/I> milk in ours.)<\/P>\n<P>But listen, now you can have your pancake and eat it, too. With the SnapJack\u2019s spring-activated handle you can quickly and easily cut your pancakes into eight equal pie-shaped pieces. Tired of having to throw away perfectly good pancakes simply because you couldn\u2019t cut them into equally-shaped sizes? Those days are long-gone.<\/P>\n<TABLE id=\"EPD\" 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>. Our favorite part of the SnapJack Pancake Cutter is actually the accompanying ad copy: \u201cWith one simple press of the handle the \u2018safe\u2019 plastic blades cut through your stack of pancakes.\u201d That\u2019s nice. But why is the word \u201csafe\u201d in quotation marks?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In case you\u2019re wondering, at Microsoft we\u2019ve discontinued research on our version of the Pancake Cutter. Our prototype actually worked pretty well; unfortunately, people didn\u2019t like the fact that every time you pressed down on the handle the Pancake Cutter would say, \u201cSomeone is trying to cut your pancakes into eight equal pie-shaped pieces. Do you want to allow this?\u201d <\/P>\n<TABLE id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note to our friends and colleagues and Microsoft<\/B>. We\u2019re just <I>joking<\/I>. After all, Microsoft would never spend all that time and effort on developing a Pancake Cutter.<\/P>\n<P>Instead, we\u2019d just go out and buy the company that makes the SnapJack cutter.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>You know, maybe we\u2019d be better off forgetting about pancake cutters and focusing on our own work. For a change. <\/P>\n<TABLE id=\"ELE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. That work happens to including writing about scripting. We thought we should clarify that for all of you who\u2019ve never been able to figure out what it is the Scripting Guys actually <I>do<\/I>.<\/P>\n<P>Or at least what we\u2019re <I>supposed<\/I> to do.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We\u2019re assuming, IU, that you have an HTA that looks something like this:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/yellowbox.jpg\" width=\"332\" height=\"252\"> \n<P><BR>As you can see, there\u2019s not much to this HTA. In fact, the HTA consists solely of three text boxes, one labeled <B>Name<\/B> (although we named the text box UserName, to prevent any problems with using reserved keywords like <I>Name<\/I>); one labeled <B>Address<\/B>; and one labeled <B>Phone<\/B>. In this case, Name is the only mandatory field; therefore, we\u2019ve colored that text box a nice, bright yellow, leaving the remaining two text boxes the boring old default color, white.<\/P>\n<P>So <I>how<\/I> did we color the Name text box a nice, bright yellow? Why, like this, of course:<\/P><PRE class=\"codeSample\">&lt;body&gt;\n    Name&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;UserName&#8221; size=&#8221;25&#8243; style=&#8221;background-color:yellow&#8221;&gt;&lt;p&gt;<\/p>\n<p>    Address&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Address&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;<\/p>\n<p>    Phone&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Phone&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Like we said, there\u2019s not much to this HTA. As we noted, we have three labels and three text boxes; the HTML tagging for the Address text box looks like this:<\/P><PRE class=\"codeSample\">Address&lt;br&gt;\n&lt;input type=&#8221;text&#8221; name=&#8221;Address&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n<\/PRE>\n<P>That\u2019s about as basic as you can get; after indicating that we want to create a text box (<B>input type=&#8221;text&#8221;<\/B>) we tack on two 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>name=&#8221;Address&#8221;<\/B>, which sets the name for the control.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>size=&#8221;25&#8243;<\/B>, which sets the size of the box. In this case the size is (approximately) the width of the text box, in characters.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>The tagging for the Phone text box is very similar. In fact, even the tagging for the Name text box is very similar, with one important exception:<\/P><PRE class=\"codeSample\">Name&lt;br&gt;\n&lt;input type=&#8221;text&#8221; name=&#8221;UserName&#8221; size=&#8221;25&#8243; style=&#8221;background-color:yellow&#8221;&gt;&lt;p&gt;\n<\/PRE>\n<P>Most likely you\u2019ve already identified that important exception. If you haven\u2019t, we\u2019ll give you a hint; it\u2019s the <B>style<\/B> parameter:<\/P><PRE class=\"codeSample\">style=&#8221;background-color:yellow&#8221;\n<\/PRE>\n<P><I>That\u2019s<\/I> how we made our text box a nice, bright yellow: we added the style parameter, and set the value of the <B>background-color<\/B> property to yellow. Would you rather have a BlanchedAlmond- or PapayaWhip-colored text box instead? Then just set the value of the background-color property accordingly:<\/P><PRE class=\"codeSample\">style=&#8221;background-color:papayawhip&#8221;\n<\/PRE>\n<TABLE id=\"E1G\" 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>. Yes, now that you mention it, Scripting Guy Dean Tsaltas <I>was<\/I> a boxer at one time in his life. But as far we know he never fought under the name Papaya Whip.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That <I>was<\/I> easy, wasn\u2019t it? In fact, that was about as easy as cutting pancakes with the SnapJack Pancake Cutter. (Just in case the good folks at SnapJack are looking for a spokesperson.) As it is, it might have been a little <I>too<\/I> easy; we still have a few minutes to kill before lunch. With that in mind, let\u2019s show you a little variation on our colored-text box code:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub SetNameColor\n         If Len(UserName.Value) &lt;&gt; 0 Then\n             UserName.style.background = &#8220;cyan&#8221;\n         Else\n             UserName.style.background = &#8220;yellow&#8221;\n         End If\n     End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    Name&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;UserName&#8221; size=&#8221;25&#8243; style=&#8221;background-color:yellow&#8221; onChange=&#8221;SetNameColor&#8221;&gt;&lt;p&gt;<\/p>\n<p>    Address&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Address&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;<\/p>\n<p>    Phone&lt;br&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Phone&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>So how does this HTA code differ from the HTA code we showed you a few minutes ago? Well, for starters, notice that we added another parameter to the UserName box. In fact, we added <I>this<\/I> parameter: <\/P><PRE class=\"codeSample\">onChange=&#8221;SetNameColor&#8221;\n<\/PRE>\n<P>This simply tells the HTA that any time the value in the UserName text box changes (that is, any time UserName\u2019s <B>onChange<\/B> event is fired) then the HTA should run the subroutine SetNameColor.<\/P>\n<P>And what does SetNameColor do? Well, the first thing it does is use the VBScript <B>Len<\/B> function to count the number of characters in the UserName text box:<\/P><PRE class=\"codeSample\">If Len(UserName.Value) &lt;&gt; 0 Then\n<\/PRE>\n<P>If Len returns a value other than 0 that means that something has been entered into the text box; in that case, the subroutine then uses this line of code to change the background color of the text box to cyan:<\/P><PRE class=\"codeSample\">UserName.style.background = &#8220;cyan&#8221;\n<\/PRE>\n<TABLE id=\"EFAAC\" 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>. Yes, that is correct. When changing the color of a text box programmatically you refer to the <B>background<\/B> property rather than the background-color property.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But what if Len <I>does<\/I> return a 0? Well, that means that nothing has been entered in the text box. (For example, someone entered a name, then thought better of it and went in and deleted that value.) In that case, the subroutine uses this line of code to reset the background color to yellow:<\/P><PRE class=\"codeSample\">UserName.style.background = &#8220;yellow\n<\/PRE>\n<P>We have no idea how useful that might be to anyone. But it was kind of fun to play around with. Just like the SnapJack Pancake Cutter. (Hint, hint.)<\/P>\n<TABLE id=\"E1AAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. We should probably make clear that the Scripting Guys have nothing against the Snapjack Pancake Cutter. In fact, the SnapJack actually looks like a pretty nice little product; we\u2019re just not sure it holds a lot of interest for the typical system administration scripter. So what <I>does<\/I> hold a lot of interest for the typical system administration scripter? To learn the answer, stay tuned to the Script Center; sometime in the next couple days we\u2019ll bring you a holiday gift guide that should be a little more to your liking.<\/P>\n<P>Well, unless you eat an awful lot of pancakes, that is. In that case, the SnapJack Pancake Cutter might be just exactly what you were looking for.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have an HTA that we use for data entry. On this form, some of the fields are mandatory and some of them are optional. I\u2019d like to indicate the mandatory fields by making those text boxes a different color than the text boxes used for the optional fields. How do I [&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-63403","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! I have an HTA that we use for data entry. On this form, some of the fields are mandatory and some of them are optional. I\u2019d like to indicate the mandatory fields by making those text boxes a different color than the text boxes used for the optional fields. How do I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63403","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=63403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63403\/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=63403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}