Created
April 8, 2013 22:49
-
-
Save cobbman/5341247 to your computer and use it in GitHub Desktop.
Nifty jQuery for clearing values of forms when focused
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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