{"id":66533,"date":"2006-09-06T17:51:00","date_gmt":"2006-09-06T17:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/06\/how-can-i-alternate-row-colors-in-an-hta\/"},"modified":"2006-09-06T17:51:00","modified_gmt":"2006-09-06T17:51:00","slug":"how-can-i-alternate-row-colors-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-alternate-row-colors-in-an-hta\/","title":{"rendered":"How Can I Alternate Row Colors in an HTA?"},"content":{"rendered":"<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\"> \n<P>Hey, Scripting Guy! How can I alternate row colors (using the green\/white style) in an HTA?<BR><BR>&#8212; MK<\/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, MK. You know, we almost didn\u2019t answer you question. Not because it wasn\u2019t a good question, but simply because \u2013 like all the questions we get \u2013 we received this one in email. Based on what happened recently at <A href=\"http:\/\/www.techweb.com\/wire\/ebiz\/192500912;jsessionid=HEOWL2XGTCPU0QSNDLOSKH0CJUNN2JVN\" target=\"_blank\">Radio Shack<\/A>, the Scripting Guys are a little leery about reading their email these days. And for good reason.<\/P>\n<TABLE id=\"EAD\" 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>. Actually, we <I>like<\/I> reading our email: it\u2019s heartening to see how many people care about the Scripting Guys. For example, nearly every message we get starts out \u201cWhat is <I>wrong<\/I> with you guys?\u201d We\u2019d just like to say that we\u2019re all doing fine. But thank you for asking; it brightens our day to know that you worry about us like that!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, having gone ahead and read your email, we don\u2019t have much choice except to try and answer it, do we? (Hmmm, maybe that\u2019s another reason why we shouldn\u2019t read our email \u2026.) Correct us if we\u2019re wrong (note to the people who do the hiring \u2013 and firing \u2013 at Microsoft: the Scripting Guys are <I>never <\/I>wrong), but it sounds like you want to dynamically create a table in an HTML Application (HTA). But not just any table; instead, you want a table where the rows are displayed in alternate colors (green, then white, then green, then white, then \u2026.). Can you do that? You bet you can. And while there are probably a number of different ways to accomplish that feat we chose this approach:<\/P><PRE class=\"codeSample\">&lt;html&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub CreateTable\n        strHTML = &#8220;&lt;table width=&#8217;100%&#8217; border&gt;&#8221;<\/p>\n<p>        For i = 1 to 11\n            x = i Mod 2\n            If x = 0 Then\n                strHTML = strHTML &amp; &#8220;&lt;tr&gt;&#8221;\n            Else\n                strHTML = strHTML &amp; &#8220;&lt;tr bgcolor=&#8217;lightgreen&#8217;&gt;&#8221;\n            End If\n            strHTML = strHTML &amp; &#8220;&lt;td width=&#8217;100%&#8217;&gt;&amp;nbsp;&lt;\/td&gt;&#8221;\n            strHTML = strHTML &amp; &#8220;&lt;\/tr&gt;&#8221;\n        Next<\/p>\n<p>        strHTML = strHTML &amp; &#8220;&lt;\/table&gt;&#8221;\n        TableArea.InnerHTML = strHTML\n    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Create Table&#8221; name=&#8221;run_button&#8221; onClick=&#8221;CreateTable&#8221;&gt;&lt;p&gt;\n    &lt;span id=TableArea&gt;&lt;\/span&gt;\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;\n<\/PRE>\n<P>What\u2019s that? Explain how this all works? Sorry; we don\u2019t have time for that. Besides, what are they going to do if we <I>don\u2019t<\/I> explain how this all works: fire us?<\/P>\n<P>On second thought, maybe we should explain how this all works. To begin with, the HTA is about as simple as we could make it. For example, the body of the HTA consists of just two items: a button that, when clicked runs a subroutine named CreateTable; and a span with the ID TableArea. As you can probably guess, that\u2019s the portion of the HTA window where we\u2019ll create our table.<\/P>\n<P>Here\u2019s what the &lt;BODY&gt; tag, in all its limited glory, looks like:<\/P><PRE class=\"codeSample\">&lt;body&gt;\n    &lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Create Table&#8221; name=&#8221;run_button&#8221; onClick=&#8221;CreateTable&#8221;&gt;&lt;p&gt;\n    &lt;span id=TableArea&gt;&lt;\/span&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>The true excitement takes place in the subroutine CreateTable, where we dynamically create the green-and-white striped table. In order to do that we need to construct the table in memory and then write the completed table to the HTA. Step 1 in that process is to assign the starting tag for the table to a variable named strHTML:<\/P><PRE class=\"codeSample\">strHTML = &#8220;&lt;table width=&#8217;100%&#8217; border&gt;&#8221;\n<\/PRE>\n<P>Now comes the fun part. For demonstration purposes we\u2019re going to create a table consisting of 11 blank rows. With that in mind, we begin by setting up a For Next loop that runs from 1 to 11:<\/P><PRE class=\"codeSample\">For i = 1 to 11\n<\/PRE>\n<P>We then have this unusual line of code:<\/P><PRE class=\"codeSample\">x = i Mod 2\n<\/PRE>\n<P>As it turns out, the <B>Mod<\/B> operator divides two numbers (in this case, it divides the counter variable i by 2) and then returns the remainder. (Strange but true.) In other words, the first time we run through our loop the counter variable i is equal to 1. Therefore, this line of code divides 1 by 2, which results in zero with a remainder of 1, and stores the remainder (1) in the variable x.<\/P>\n<P>Why the heck would we do <I>that<\/I>? Well, we want to alternate row colors: odd-numbered rows will be green, even-numbered rows will be white. One really easy way to determine whether a number is even or odd is to divide it by 2 and take a peek at the remainder. If the remainder is 0 then we have an even number; if the remainder is anything <I>but<\/I> 0 then we have an odd number. Seeing as how the Mod operator returns just the remainder we can determine whether a number (and thus a row in the table) is odd or even simply by checking to see if x is equal to 0:<\/P><PRE class=\"codeSample\">If x = 0 Then\n<\/PRE>\n<P>Suppose we <I>do<\/I> have an even number; what then? Well, if the remainder is equal to 0 we simply tack on code for creating a new table row to the variable strHTML:<\/P><PRE class=\"codeSample\">strHTML = strHTML &amp; &#8220;&lt;tr&gt;&#8221;\n<\/PRE>\n<P>And if the remainder is <I>not<\/I> equal to 0, meaning we have an odd number? In that case we also tack on code to create a new table row; the only difference is that, this time around, we include the <B>bgcolor<\/B> parameter and set the background color of the row to lightgreen:<\/P><PRE class=\"codeSample\">strHTML = strHTML &amp; &#8220;&lt;tr bgcolor=&#8217;lightgreen&#8217;&gt;&#8221;\n<\/PRE>\n<P>See how that works? For even-numbered rows we create a new row without specifying a background color; for odd-numbered rows we create a new row and set the background color to lightgreen. The net effect: we\u2019ll end up with alternating rows of green and white.<\/P>\n<P>After creating the new row we use this code to create a new table cell (a cell containing a blank space, as indicated by the <B>&amp;nbsp<\/B> construction), then use the &lt;\/tr&gt; tag to close off the row:<\/P><PRE class=\"codeSample\">strHTML = strHTML &amp; &#8220;&lt;td width=&#8217;100%&#8217;&gt;&amp;nbsp;&lt;\/td&gt;&#8221;\nstrHTML = strHTML &amp; &#8220;&lt;\/tr&gt;&#8221;\n<\/PRE>\n<P>With row number 1 finished we then loop around and repeat the process until we\u2019ve added 11 rows to our table. That brings us to these two lines of code:<\/P><PRE class=\"codeSample\">strHTML = strHTML &amp; &#8220;&lt;\/table&gt;&#8221;\nTableArea.InnerHTML = strHTML\n<\/PRE>\n<P>In the first line we add the &lt;\/table&gt; tag to the variable strHTML; that simply indicates that the table is now complete. We then use the second line of code to set the <B>InnerHTML<\/B> property of the TableArea span to the value of strHTML (strHTML, of course, contains all the HTML tagging required to create a green-and-white striped table). The moment we do that our HTA should look like this:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/stripes.jpg\" width=\"299\" height=\"409\"> \n<P><BR>Which is pretty much what we wanted it to look like.<\/P>\n<TABLE id=\"EWF\" 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, we know: &lt;SPAN&gt; tags and InnerHTML properties; all that stuff can be terribly confusing if you\u2019re new to HTAs or to dynamic HTML. In that case you might take a peek at some of the articles in the <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>There you have it, MK; that should be enough to get you started. As for us, we need to buckle down and get to work. Sure, it\u2019s easy to say \u201cThe Scripting Guys? Microsoft would never fire the Scripting Guys.\u201d But that\u2019s what they said about <A href=\"http:\/\/null\/presspass\/features\/2001\/apr01\/04-11clippy.mspx\" target=\"_blank\">Clippy the Paperclip<\/A>, too. If Microsoft is willing to fire Clippy, well, it\u2019s hard to believe that anyone\u2019s job could be safe around here. If you need us we\u2019ll be in our office. Working.<\/P>\n<P>Yes, yes: for once.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I alternate row colors (using the green\/white style) in an HTA?&#8212; MK Hey, MK. You know, we almost didn\u2019t answer you question. Not because it wasn\u2019t a good question, but simply because \u2013 like all the questions we get \u2013 we received this one in email. Based on what happened [&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-66533","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 alternate row colors (using the green\/white style) in an HTA?&#8212; MK Hey, MK. You know, we almost didn\u2019t answer you question. Not because it wasn\u2019t a good question, but simply because \u2013 like all the questions we get \u2013 we received this one in email. Based on what happened [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66533","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=66533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66533\/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=66533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}