Skip to content

Instantly share code, notes, and snippets.

@carlosantoniodasilva
Created September 25, 2011 14:54
Show Gist options
  • Save carlosantoniodasilva/1240679 to your computer and use it in GitHub Desktop.
Save carlosantoniodasilva/1240679 to your computer and use it in GitHub Desktop.
Simple placeholder js
var Placeholder = {
init: function(selector) {
if (!this.supportPlaceholder()) {
$(selector).placeholder();
}
},
supportPlaceholder: function() {
return 'placeholder' in document.createElement('input');
}
}
$.fn.placeholder = function() {
var object = this,
focus = function() {
if (object.val() == object.attr('placeholder')) { object.val(''); }
},
blur = function() {
if (!$.trim(object.val())) { object.val(object.attr('placeholder')); }
};
blur.call(this);
return this.focus(focus).blur(blur);
}
$(function() {
Placeholder.init('input[placeholder]');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment