We help IT Professionals succeed at work.

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

Murray Brown
Murray Brown asked
on
86 Views
Last Modified: 2018-08-22
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

MishaProgrammer
CERTIFIED EXPERT

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/VBA/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?
MishaProgrammer
CERTIFIED EXPERT

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?
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Murray BrownASP.net/VBA/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
MishaProgrammer
CERTIFIED EXPERT

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/VBA/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
CERTIFIED EXPERT
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/VBA/VSTO Developer

Author

Commented:
Thanks Kaufm3d. Is the <input> method better on most browsers?
CERTIFIED EXPERT
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.