Skip to content

Instantly share code, notes, and snippets.

@Schubidu
Created March 6, 2012 10:45
Show Gist options
  • Save Schubidu/1985641 to your computer and use it in GitHub Desktop.
Save Schubidu/1985641 to your computer and use it in GitHub Desktop.
jquery placeholder fallback
(function ($) {
$.support.placeholder = false;
var test = document.createElement('input');
if ('placeholder' in test) $.support.placeholder = true;
$.fn.placeholderFallback = function () {
if (!$.support.placeholder) {
var active = document.activeElement;
this.focus(
function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(
function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
}).blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function () {
$(this).val('');
});
});
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment