Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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?

User generated image
Avatar of Misha
Misha
Flag of Russian Federation image

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

Avatar of Murray Brown

ASKER

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?
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?
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Thanks for clarifying that the <input> method is the best answer Midsha and thanks very much Ryan for a great answer
Always keep in mind the availability of these kind of things for cross-browser, if you are in an environment that supports multiple browsers.
Thanks Kaufm3d. Is the <input> method better on most browsers?
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.