Skip to content

Instantly share code, notes, and snippets.

@hsquareweb
Created April 19, 2012 23:04
Show Gist options
  • Save hsquareweb/2424772 to your computer and use it in GitHub Desktop.
Save hsquareweb/2424772 to your computer and use it in GitHub Desktop.
jQuery: Placeholder Fallback For Modernizer
//Placeholders fallback
$(function() {
// check placeholder browser support
if (!Modernizr.input.placeholder) {
// set placeholder values
$(this).find('[placeholder]').each(function() {
if ($(this).val() == '') { $(this).val( $(this).attr('placeholder') ); }
});
// focus and blur of placeholders
$('[placeholder]').focus(function() {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
$(this).removeClass('placeholder');
}
}).blur(function() {
if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
$(this).val($(this).attr('placeholder'));
$(this).addClass('placeholder');
}
});
// remove placeholders on submit
$('[placeholder]').closest('form').submit(function() {
$(this).find('[placeholder]').each(function() {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
}
})
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment