How to Clear Gravity Forms Field on Click or Focus

One of the most common requests I receive as a WordPress developer is to make it so that a simple form field value clears when a user clicks or focuses on a given field.  Since I use Gravity Forms for just about ever form I create, I would like to provide the following code, which was provided via the Gravity Forms forums:

jQuery(document).ready(function() {
	jQuery.fn.cleardefault = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};
jQuery(".clearit input, .clearit textarea").cleardefault();
});

Note that you will need to add the “clearit” class to each form field via the Gravity Forms back end.

Posted in

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress consultant, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and then to twitter.com/tobycryns and twitter.com/themightymo

Follow Toby's contributions on Github and WP.org.