Skip to content

Instantly share code, notes, and snippets.

@handerson
Created June 14, 2010 14:45
Show Gist options
  • Save handerson/437775 to your computer and use it in GitHub Desktop.
Save handerson/437775 to your computer and use it in GitHub Desktop.
jQuery plugin that clears and restores a field's default value
// clears and restores a field's default value
// example usage (js): $('input.has_default').hasDefaultValue();
// example usage (html): <input class="has_default" default="This is displayed by default" type="text"/>
jQuery.fn.hasDefaultValue = function() {
function supports_input_placeholder() {
var i = document.createElement('input');
return 'placeholder' in i;
}
if(!supports_input_placeholder()){
this.each(function(){
if(this.value === ""){
this.value = $(this).attr("placeholder")
}
});
this.focus(function(event){
if(this.value === $(this).attr("placeholder")){
this.value = ""
}
})
this.blur(function(event){
if($(this).attr("placeholder") && this.value === ""){
this.value = $(this).attr("placeholder");
}
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment