How do I make a textbox that has a grayed out content, and when I click on it to enter text, the grayed out portion, it disappears and allows me to enter the desired text?
Example:
A "First Name" text box. The words "First Name" are inside the text box grayed out, when I click, those words disappear and I write my name in it.
If you're targeting HTML5 only you can use:
For non HTML5 browsers, I would build upon Floern's answer by using jQuery and make the javascript non-obtrusive. I would also use a class to define the blurred properties.
Functions:
CSS:
This is an elaborate version, to help you understand
Use like this:
This answer illustrates a pre-HTML5 approach. Please take a look at Psytronic's answer for a modern solution using the
placeholder
attribute.HTML:
JavaScript:
With HTML5, you can do this natively with:
<input name="first_name" placeholder="First Name">
This is not supported with all browsers though (IE)
This may work:
Otherwise, if you are using jQuery, you can use .focus and .css to change the color.
This works:
Note: You can change the placeholder, id and type value to "email" or whatever suits your need.
More details by W3Schools at:http://www.w3schools.com/tags/att_input_placeholder.asp
But by far the best solutions are by Floern and Vivek Mhatre ( edited by j0k )
I have a code snippet below, that is a typical web page.
The shortest way is to directly add the below code as additional attributes in the input type that you want to change.
Please note: Change the text "Search" to "go" or any other text to suit your requirements.