Created
February 22, 2011 11:37
-
-
Save sjmulder/838548 to your computer and use it in GitHub Desktop.
Graceful degradation for browser not supporting the placeholder attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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