Skip to content

Instantly share code, notes, and snippets.

@jlengstorf
Created April 30, 2012 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlengstorf/2555001 to your computer and use it in GitHub Desktop.
Save jlengstorf/2555001 to your computer and use it in GitHub Desktop.
Placeholder fallback using Modernizr
// check placeholder browser support
if( !Modernizr.input.placeholder )
{
$(this).find('[placeholder]').each(function() {
if( $(this).val() == '' ) // if field is empty
{
$(this).val( $(this).attr('placeholder') );
}
});
$('[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');
}
})
.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