ASP.net make text in a webform text box dissapear whe the user clicks in it

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

In my ASP.net web app I have text boxes that allow the user to enter the start and end of a range.
The image below shows this.
I have put the words "From" and "To" to make it easier for new users.
I want these words to disappear if the user clicks in one of the text boxes.
How do I achieve this in ASP.net?

1
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
There in no image,,.
If you want to do this on client side you can use javascript and this code:
<asp:TextBox onclick="txtBox1_ClientClicked()" ID="TextBox1" runat="server" OnClick="TextBox1_Click"></asp:TextBox>
<script type="text/javascript">
    function txtBox1_ClientClicked(){
       // do something
//
        document.getElementById("TextBox1").value = "";
    }
</script>

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks Misha. I have several textboxes. How would I change that javascript so that it would affect the text box using it? Do I put that code in the <head> section of the markup?

Commented:
May be I don`t understand you.. This code have affect to your TextBox1. When you click  on this textbox to input text, its value automatically changed on "".
You can put javascript code to other js file and add link to this file in section head like this:
<script type="text/javascript" src="/scripts/YourTextBoxScript.js"></script>

Open in new window


What do you mean?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Software Team Lead
Commented:
or HTML placeholder serves your purpose?

HTML <input> placeholder Attribute
https://www.w3schools.com/tags/att_input_placeholder.asp

you no need to add any javascript to implement this effect.
Murray BrownASP.net/VSTO Developer

Author

Commented:
So Misha all I mean is that I want to call that JavaScript from any TextBox. I have 14 of them and it will clear that box.
It works well with TextBox1

Commented:
Can you add event
 onclick="txtBox1_ClientClicked()"

Open in new window

to each textBox? And handle it in one place (javascript)
But Ryan Chong solution (placeholder)  is better.
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks for clarifying that the <input> method is the best answer Midsha and thanks very much Ryan for a great answer
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Always keep in mind the availability of these kind of things for cross-browser, if you are in an environment that supports multiple browsers.
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks Kaufm3d. Is the <input> method better on most browsers?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
It's certainly available on most browsers; but as you can see in that link I posted, some browsers still require vendor-specific tags (e.g. :-ms-input-placeholder for IE 11) in order to get placeholder to work correctly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial