Skip to content

Instantly share code, notes, and snippets.

@sjmulder
Created February 22, 2011 11:37
Show Gist options
  • Save sjmulder/838548 to your computer and use it in GitHub Desktop.
Save sjmulder/838548 to your computer and use it in GitHub Desktop.
Graceful degradation for browser not supporting the placeholder attribute
var setupPlaceholders = function () {
if (typeof document.createElement('input').placeholder !== 'undefined') {
return;
}
var clearPlaceholder = function () {
var input = jQuery(this);
if (input.hasClass('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
};
var onBlur = function () {
var input = jQuery(this);
if (input.val() === '') {
input.val(input.attr('placeholder'));
input.addClass('placeholder');
}
};
jQuery('input[placeholder]').each(function () {
var input = jQuery(this);
input.val(jthis.attr('placeholder'));
input.addClass('placeholder');
input.focus(clearPlaceholder);
input.blur(onBlur);
input.closest('form').submit(function () {
clearPlaceholder.apply(input);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment