Fork me on GitHub

jQuery – Clear Default Input Values Once

jQuery – Clear Default Input Values Once

I like to have default values in my text input fields. However it is an annoying user experience to have to manually clear the value out and then enter in the value. Instead I thought I would write a simple script that would work site wide to clear out the default values. You of course will need your jquery included:

Following some brilliant comments on this there is a much quicker method:

$('input[type=text]').one('focus', function(){
	$(this).attr('value', '');
});

However the original code I came up with is as follows:

$(function(){
	$('input[type=text]').focus(function(){
		if (!this.clicked) {
			$(this).attr('value', '');
			this.clicked = true;	
		}
	});
});

This will clear the fields out only once. So if the user enters a value, then focuses back on the field, their values won’t be cleared. So if they needed to edit a single letter, then their entire entry won’t be wiped. If you have any hints feel free to comment

Posted by voidet

Categorised under Programming
Bookmark the permalink or leave a trackback.

3 Comments

  1. Mick

    Instead of setting the clicked property you could just use the .one() method. http://api.jquery.com/one/

    It’s great for uses such as this…

    June 8, 2010 @ 10:27 pm
  2. Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

    June 17, 2010 @ 3:10 pm
  3. Great snippet, but how would you re-instate the default value on focusout()?

    September 25, 2010 @ 3:08 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or