Avatar of Murray Brown
Murray Brown
Flag 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?

1
ASP.NET

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
Misha

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 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?
Misha

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Murray Brown

ASKER
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
Misha

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 Brown

ASKER
Thanks for clarifying that the <input> method is the best answer Midsha and thanks very much Ryan for a great answer
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kaufmed

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 Brown

ASKER
Thanks Kaufm3d. Is the <input> method better on most browsers?
kaufmed

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy