Created
September 23, 2011 00:56
-
-
Save terkel/1236496 to your computer and use it in GitHub Desktop.
HTML5 Placeholder Polyfill using jQuery
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
(function ($) { | |
$(function () { | |
if (!supportsInputAttribute('placeholder')) { | |
$('[placeholder]').each(function () { | |
var $this = $(this), | |
$form = $this.closest('form'), | |
placeholderText = $this.attr('placeholder'), | |
placeholderColor = 'GrayText', | |
defaultColor = $this.css('color'); | |
$this.bind({ | |
focus: function () { | |
if ($this.val() === placeholderText) { | |
$this.val('').css('color', defaultColor); | |
} | |
}, | |
blur: function () { | |
if ($this.val() === '') { | |
$this.val(placeholderText).css('color', placeholderColor); | |
} else if ($this.val() === placeholderText) { | |
$this.css('color', placeholderColor); | |
} | |
} | |
}); | |
$this.trigger('blur'); | |
$form.submit(function () { | |
if ($this.val() === placeholderText) { | |
$this.val(''); | |
} | |
}); | |
}); | |
} | |
}); | |
// detect support for input attirbute | |
function supportsInputAttribute (attr) { | |
var input = document.createElement('input'); | |
return attr in input; | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment