Skip to content

Instantly share code, notes, and snippets.

@jeremymouton
Created February 19, 2013 17:22
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 jeremymouton/4987942 to your computer and use it in GitHub Desktop.
Save jeremymouton/4987942 to your computer and use it in GitHub Desktop.
Input placeholder support for old IE browsers
// Placeholder support for IE and older browsers.
// http://www.femgeek.co.uk/html5-placeholders-for-troublesome-browsers-ie-ie9/
$.support.placeholder = ('placeholder' in document.createElement('input'));
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$('textarea').each(function(index, element) {
if($(this).val().length === 0) {
$(this).html($(this).attr('id')).addClass('hasPlaceholder');
}
});
$('input, textarea').focus(function () {
if ($(this).attr('placeholder') !== '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder error');
}}).blur(function () {
if (($(this).attr('placeholder') !== '' && ($(this).val() === '' || $(this).val() == $(this).attr('placeholder')))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment