Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created December 8, 2011 00:30
Show Gist options
  • Save tkaemming/1445506 to your computer and use it in GitHub Desktop.
Save tkaemming/1445506 to your computer and use it in GitHub Desktop.
(function ($) {
$.fn.placeholder = function (_options) {
var defaults = {
blank: '',
className: 'default'
},
options = $.extend({}, defaults, _options || {});
return this.each(function () {
var node = $(this);
if (!Modernizr.input.placeholder) {
var placeholder = node.attr('placeholder');
// Don't bind the placeholder events more than once.
node.unbind('.placeholder')
if (node.val() === options.blank || node.val() === placeholder) {
node.val(placeholder).addClass(options.className);
}
node.bind('focus.placeholder', function (event) {
if (node.val() === placeholder) {
node.val(options.blank).removeClass(options.className);
}
});
node.bind('blur.placeholder', function (event) {
if (node.val() === options.blank || node.val() === placeholder) {
node.val(placeholder).addClass(options.className);
}
});
node.closest('form').bind('submit.placeholder', function (event) {
if (node.val() === placeholder) {
node.val(options.blank).removeClass(options.className);
}
});
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment