Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete default value of an input text on click

I have an input text :

<input name="Email" type="text" id="Email" value="[email protected]" /> 

I want to put a default value like "What's your programming question ? be specific." in StackOverFlow, and when the user click on it the default value disapear.

like image 549
Wassim AZIRAR Avatar asked Jun 06 '10 13:06

Wassim AZIRAR


People also ask

How do you remove input field values?

To clear an input field after submitting: When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

How do you delete input fields in text?

The “reset()” method clears all the values of the form elements. This method can be implemented to clear the input field holding text with a click of a button.


2 Answers

For future reference, I have to include the HTML5 way to do this.

<input name="Email" type="text" id="Email" value="[email protected]" placeholder="What's your programming question ? be specific." /> 

If you have a HTML5 doctype and a HTML5-compliant browser, this will work. However, many browsers do not currently support this, so at least Internet Explorer users will not be able to see your placeholder. However, see http://www.kamikazemusic.com/quick-tips/jquery-html5-placeholder-fix/ (archive.org version) for a solution. Using that, you'll be very modern and standards-compliant, while also providing the functionality to most users.

Also, the provided link is a well-tested and well-developed solution, which should work out of the box.

like image 183
MvanGeest Avatar answered Sep 28 '22 03:09

MvanGeest


EDIT: Although, this solution works, I would recommend you try MvanGeest's solution below which uses the placeholder-attribute and a javascript fallback for browsers which don't support it yet.

If you are looking for a Mootools equivalent to the JQuery fallback in MvanGeest's reply, here is one.

--

You should probably use onfocus and onblur events in order to support keyboard users who tab through forms.

Here's an example:

<input type="text" value="[email protected]" name="Email" id="Email"  onblur="if (this.value == '') {this.value = '[email protected]';}"  onfocus="if (this.value == '[email protected]') {this.value = '';}" /> 
like image 37
mqchen Avatar answered Sep 28 '22 03:09

mqchen