Skip to content

Instantly share code, notes, and snippets.

@cobbman
Created April 8, 2013 22:49
Show Gist options
  • Save cobbman/5341247 to your computer and use it in GitHub Desktop.
Save cobbman/5341247 to your computer and use it in GitHub Desktop.
Nifty jQuery for clearing values of forms when focused
var formElements = '.formClass input, .formClass textarea, #anyID';
$(document).ready(function(){
$(formElements).each(function(){ //do this for each element
$.data(this, 'default', this.value); //grab the default value and pair with 'default'
}).focus(function(){
if ( $.data(this, 'default') === $(this).val() ) { // on focus: clear the value if it's the default
$(this).val('');
}
}).blur(function(){
if ( $(this).val() === '' ) {
$(this).val($.data(this, 'default')); // on blur: put default value back if blank
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment