Skip to content

Instantly share code, notes, and snippets.

@citrus
Created June 15, 2011 21:04
Show Gist options
  • Save citrus/1028100 to your computer and use it in GitHub Desktop.
Save citrus/1028100 to your computer and use it in GitHub Desktop.
jQuery Textfield-Value Labels
// jQuery Textfield Labels
// Valabel == Value + Label
// by Spencer Steffen
// Citrus Media Group
// [email protected]
;(function($) {
var version = '0.1.0';
$.fn.valabel = function() {
var args = arguments;
return this.each(function() {
var options = this.options = $.extend(false, this.options || $.fn.valabel.defaults, (args[0] && args[0].constructor == Object ? args[0] : {}));
var self = this.self || $(this).addClass(options.class_name);
var text = this.text || options.text || self.val();
if (self.val() != text) {
self.val(text);
}
self.focus(function() {
if (self.val() == text) {
self.val("").removeClass(options.class_name);
}
}).blur(function() {
if (self.val() == "") {
self.val(text).addClass(options.class_name);
}
});
return this;
});
};
$.fn.valabel.defaults = {
class_name: 'light',
text: null
};
})(jQuery);
$(document).ready(function() {
$("#keywords, #search_keywords").valabel();
$("#email").valabel({ class_name: "blur", :text => "Enter your email" });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment