Skip to content

Instantly share code, notes, and snippets.

@rmehner
Created January 13, 2012 13:52
Show Gist options
  • Save rmehner/1606322 to your computer and use it in GitHub Desktop.
Save rmehner/1606322 to your computer and use it in GitHub Desktop.
A little placeholder jQuery snippet I've used for years now
jQuery(function($) {
var input = document.createElement('input');
var placeholderSupport = 'placeholder' in input;
// prevent IE from leaking
input = null;
if (placeholderSupport) {
return;
}
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('').removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder').val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment