Skip to content

Instantly share code, notes, and snippets.

@drywall
Created October 16, 2009 22:37
Show Gist options
  • Save drywall/212128 to your computer and use it in GitHub Desktop.
Save drywall/212128 to your computer and use it in GitHub Desktop.
/**
* jQuery Resettable - A function to make form fields reset to their initial values when left blank
* Copyright (c) 2009 Ben Byrne - ben(at)fireflypartners(dot)com | http://www.fireflypartners.com
* Dual licensed under MIT and GPL.
* Date: 10/16/2009
* @author Ben Byrne
* @version 0.1.0
*
*/
jQuery.fn.resettable = function ( ) {
fields = new Array();
this.each(function() {
//check to make sure the element has a name attribute. If not, do nothing!
if (!jQuery(this).attr("name")) return true;
fields [ jQuery(this).attr("name") ] = jQuery(this).val();
jQuery(this).focus(function() {
if ( jQuery(this).val() == fields[ jQuery(this).attr("name") ] ) jQuery(this).val("");
}).blur(function() {
if (jQuery(this).val() == "") jQuery(this).val( fields[ jQuery(this).attr("name") ] );
});
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment