Skip to content

Instantly share code, notes, and snippets.

@ricardoriogo
Created August 31, 2014 00:52
Show Gist options
  • Save ricardoriogo/03b7c7b8199c4cd7c3c6 to your computer and use it in GitHub Desktop.
Save ricardoriogo/03b7c7b8199c4cd7c3c6 to your computer and use it in GitHub Desktop.
jQuery placeholder fallback
/**
* jQuery placeholder fallback
*/
$(document).ready(function() {
if ( !("placeholder" in document.createElement("input")) ) {
$("input[placeholder], textarea[placeholder]").each(function() {
var val = $(this).attr("placeholder");
if ( this.value == "" ) {
this.value = val;
}
$(this).focus(function() {
if ( this.value == val ) {
this.value = "";
}
}).blur(function() {
if ( $.trim(this.value) == "" ) {
this.value = val;
}
})
});
// Clear default placeholder values on form submit
$('form').submit(function() {
$(this).find("input[placeholder], textarea[placeholder]").each(function() {
if ( this.value == $(this).attr("placeholder") ) {
this.value = "";
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment