Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Forked from wenjul/placeholder.css
Last active December 18, 2015 19:00
Show Gist options
  • Save monkeymonk/5830036 to your computer and use it in GitHub Desktop.
Save monkeymonk/5830036 to your computer and use it in GitHub Desktop.
jQuery simple placeholder support
supportsPlaceholder = "placeholder" of document.createElement("input")
$("[placeholder]").each ->
unless supportsPlaceholder
self = $(this)
self.val(self.attr("placeholder")).addClass "placeholder" if self.val() is ""
self.focus ->
self.val("").removeClass "placeholder" if self.val() is self.attr("placeholder")
self.blur ->
self.val(self.attr("placeholder")).addClass "placeholder" if self.val() is ""
var supportsPlaceholder = 'placeholder' in document.createElement('input');
$('[placeholder]').each(function () {
if (!supportsPlaceholder) {
var self = $(this);
if (self.val() == '') {
self.val(self.attr('placeholder')).addClass('placeholder');
}
self.focus(function(){
if (self.val() == self.attr('placeholder')) {
self.val('').removeClass('placeholder');
}
});
self.blur(function(){
if (self.val() == '') {
self.val(self.attr('placeholder')).addClass('placeholder');
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment