Created
June 14, 2010 14:45
-
-
Save handerson/437775 to your computer and use it in GitHub Desktop.
jQuery plugin that clears and restores a field's default value
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
// 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