How Can I Clear a Text Box in an HTA?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I clear a text box in an HTA?

— CB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CB. You know, it’s been awhile since we’ve answered a question about HTAs (HTML Applications). And we could probably get away with that except for the fact that a month or so ago we debuted the HTA Developer’s Center, something which implied that we were going to try and do a better job of supporting people interested in writing HTAs. So have we done a better job of supporting people interested in writing HTAs? You know, that’s a very important question. Too bad we don’t have time to answer it….

But look on the bright side: today, at least, we are supporting people interested in writing HTAs. So let’s see if we can figure out how to programmatically clear a text box.

We’re assuming you have an HTA vaguely similar to this; in other words, you have a text box where users can enter data. You also have a button that makes it very easy for users to erase that data and start over again; all they have to do is click the Clear Text button and any text in the text box will be erased. In our simplified version the HTA looks like this:


There’s just one catch: how do we erase the text any time a user clicks the button?

Believe it or not that’s one of the easiest things you’ll ever do in scripting. In HTML each text box has a property named Value, which represents whatever happens to be in the text box at the moment. For example, in our sample HTA the textbox has the value This text box has text in it. If we want to get rid of that text all we have to do is set the Value property to nothing. Assuming our text box is named MyTextBox this line of code will do the trick:

MyTextBox.Value = “”

That’s all there is to it. If we wanted to change the text so it read “Please enter your name here:” we’d use this line of code:

MyTextBox.Value = “Please enter your name here:”

If you’d like to try this out on your own here’s the complete code for our sample HTA. Just copy the following into Notepad and then save the file with a .hta file extension:

<title>HTA Test</title>

<script language=”VBScript”> Sub ClearText MyTextBox.Value = “” End Sub </script>

<body> <input type=”text” name=”MyTextBox” size=40 value=”This text box has text in it”><p> <input type=”button” value=”Clear Text” name=”run_button” onClick=”ClearText”> </body>

And before you ask, yes, this same approach can also be used to clear any text typed into a text area. That’s right, with today’s Internet special you get two questions answered for the price of one. Let’s see any other daily scripting-related question-and-answer column found on the Internet beat that deal.


Comments are closed. Login to edit/delete your existing comments