Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created September 5, 2012 18:47
Show Gist options
  • Save sapegin/3642403 to your computer and use it in GitHub Desktop.
Save sapegin/3642403 to your computer and use it in GitHub Desktop.
Simplest placeholder polyfill with jQuery
if (!('placeholder' in document.createElement('input'))) {
$('input[placeholder], textarea[placeholder]').each(function() {
var input = $(this),
val = input.attr('placeholder');
if (!this.value) {
this.value = val;
}
input.focus(function() {
if (this.value === val) {
this.value = '';
}
});
input.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