Skip to content

Instantly share code, notes, and snippets.

@greypants
Created February 14, 2013 22:58
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 greypants/4957179 to your computer and use it in GitHub Desktop.
Save greypants/4957179 to your computer and use it in GitHub Desktop.
JS: Placeholder polyfill
createPlaceholders: function() {
var noPlaceholderSupport = $('html').hasClass('lte9');
if(noPlaceholderSupport) {
var createPlaceholder = function() {
var self = this;
var $this = $(this);
var isEmpty = true;
var placeholder = this.placeholder;
// Wrap in a container
$this.wrap('<div class="placeholderWrapper">');
$this.after('<div class="placeholder">' + placeholder + '</div>');
var $placeholder = $this.siblings('.placeholder');
var $wrapper = $this.parent('.placeholderWrapper');
$placeholder.on({
click: function() {
$placeholder.hide();
$this.focus();
}
});
$this.on({
'click': function(){
$placeholder.hide();
},
'keydown': function() {
$placeholder.hide();
},
'blur': function() {
if(isEmpty) {
$placeholder.show();
}
},
change: function() {
isEmpty = (self.value.length < 1);
}
});
};
$('form').find('[placeholder]').each(createPlaceholder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment