diff --git a/jquery.labelify.js b/jquery.labelify.js index 909cbd3..83860a0 100644 --- a/jquery.labelify.js +++ b/jquery.labelify.js @@ -19,83 +19,87 @@ * * @example $('input').labelify('hasLabel'); // return true if the field has a label */ -jQuery.fn.labelify = function(settings) { - // if the element has a label, return true when 'hasLabel' is passed as an arg - if (typeof settings === 'string' && settings === 'hasLabel') { - return $(this).data('hasLabel'); - } +(function($) { + $.fn.labelify = function(settings) { + // if the element has a label, return true when 'hasLabel' is passed as an arg + if (typeof settings === 'string' && settings === 'hasLabel') { + return $(this).data('hasLabel'); + } - settings = jQuery.extend({ - text: 'title', - labeledClass: '' - }, settings); + settings = $.extend({ + text: 'title', + labeledClass: '' + }, settings); - // Compatibility with version 1.3 and prior (double-ls) - if (settings.labelledClass) { settings.labeledClass = settings.labelledClass; } + // Compatibility with version 1.3 and prior (double-ls) + if (settings.labelledClass) { settings.labeledClass = settings.labelledClass; } - var showLabel, hideLabel, - lookups, lookup, - $labelified_elements; + var showLabel, hideLabel, + lookups, lookup, + $labelified_elements; - lookups = { - title: function(input) { - return $(input).attr('title'); - }, - label: function(input) { - return $("label[for=" + input.id +"]").text(); - } - }; + lookups = { + title: function(input) { + return $(input).attr('title'); + }, + label: function(input) { + return $("label[for=" + input.id +"]").text(); + } + }; - $labelified_elements = $(this); + $labelified_elements = $(this); - showLabel = function(el){ - $(el).data('value', el.value); - el.value = $(el).data("label"); - $(el).addClass(settings.labeledClass).data('hasLabel', true); - }; - hideLabel = function(el){ - el.value = $(el).data('value'); - $(el).removeClass(settings.labeledClass).data('hasLabel', false); - }; + showLabel = function(el){ + $(el).data('value', el.value); + $(el).data('maxlength', $(el).attr('maxlength')).removeAttr('maxlength'); + el.value = $(el).data("label"); + $(el).addClass(settings.labeledClass).data('hasLabel', true); + }; + hideLabel = function(el){ + $(el).attr('maxlength', $(el).data('maxlength')); + el.value = $(el).data('value'); + $(el).removeClass(settings.labeledClass).data('hasLabel', false); + }; - return $(this).each(function() { - var $item = $(this), - removeValuesOnExit; + return $(this).each(function() { + var $item = $(this), + removeValuesOnExit; - if (typeof settings.text === 'string') { - lookup = lookups[settings.text]; // what if not there? - } else { - lookup = settings.text; // what if not a fn? - } + if (typeof settings.text === 'string') { + lookup = lookups[settings.text]; // what if not there? + } else { + lookup = settings.text; // what if not a fn? + } - // bail if lookup isn't a function or if it returns undefined - if (typeof lookup !== "function" || !lookup(this)) { return; } + // bail if lookup isn't a function or if it returns undefined + if (typeof lookup !== "function" || !lookup(this)) { return; } - $item.bind('focus.label',function() { - if (this.value === $(this).data("label")) { hideLabel(this); } - }).bind('blur.label',function(){ - if (this.value == '') { showLabel(this); } - }); - $item.data('label', lookup(this).replace(/\n/g,'')) // strip label's newlines - $item.data('value', this.value); // initialise remembered value - - removeValuesOnExit = function() { - $labelified_elements.each(function(){ + $item.bind('focus.label',function() { if (this.value === $(this).data("label")) { hideLabel(this); } + }).bind('blur.label',function(){ + if (this.value == '') { showLabel(this); } }); - }; - - $item.parents("form").submit(removeValuesOnExit); - $(window).unload(removeValuesOnExit); - - if (this.value !== this.defaultValue || this.defaultValue != '') { - // user already started typing; don't overwrite their work! - // also, if a value is already set in the field, don't replace it - // with a label - return; - } + $item.data('label', lookup(this).replace(/\n/g,'')) // strip label's newlines + $item.data('value', this.value); // initialise remembered value + + removeValuesOnExit = function() { + $labelified_elements.each(function(){ + if (this.value === $(this).data("label")) { hideLabel(this); } + }); + }; - // set the defaults - showLabel(this); - }); -}; + $item.parents("form").submit(removeValuesOnExit); + $(window).unload(removeValuesOnExit); + + if (this.value !== this.defaultValue || this.defaultValue != '') { + // user already started typing; don't overwrite their work! + // also, if a value is already set in the field, don't replace it + // with a label + return; + } + + // set the defaults + showLabel(this); + }); + }; +}(jQuery)); \ No newline at end of file