How to: Automatically empty the contents of a TextBox when it gains focus

You can easily empty the contents of a textbox when you place the cursor or click the pointer inside it.

In your html, add the following inside the textbox control:

onfocus=”this.value = ( this.value == this.defaultValue ) ? ” : this.value;return true;”

That will make sure that whatever text resides inside the textbox, will be deleted once the cursor is places in the textbox control.

A full textbox tag would then be:

<asp:TextBox ID=”SearchBox” runat=”server” Text=”Search” onfocus=”this.value = ( this.value == this.defaultValue ) ? ” : this.value;return true;” />

Easy.

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.