2 * @license In-Field Label jQuery Plugin
3 * http://fuelyourcoding.com/scripts/infield.html
5 * Copyright (c) 2009-2010 Doug Neiner
6 * Dual licensed under the MIT and GPL licenses.
7 * Uses the same license as jQuery, see:
8 * http://docs.jquery.com/License
14 $.InFieldLabels = function (label, field, options) {
15 // To avoid scope issues, use 'base' instead of 'this'
16 // to reference this class from internal events and functions.
19 // Access to jQuery and DOM versions of each element
20 base.$label = $(label);
23 base.$field = $(field);
26 base.$label.data("InFieldLabels", base);
29 base.init = function () {
30 // Merge supplied options with default options
31 base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);
33 // Check if the field is already filled in
34 if (base.$field.val() !== "") {
39 base.$field.focus(function () {
42 base.checkForEmpty(true);
43 }).bind('keydown.infieldlabel', function (e) {
44 // Use of a namespace (.infieldlabel) allows us to
45 // unbind just this method later
47 }).bind('paste', function (e) {
48 // Since you can not paste an empty string we can assume
49 // that the fieldis not empty and the label can be cleared.
51 }).change(function (e) {
53 }).bind('onPropertyChange', function () {
58 // If the label is currently showing
59 // then fade it down to the amount
60 // specified in the settings
61 base.fadeOnFocus = function () {
63 base.setOpacity(base.options.fadeOpacity);
67 base.setOpacity = function (opacity) {
68 base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
69 base.showing = (opacity > 0.0);
72 // Checks for empty as a fail safe
73 // set blur to true when passing from
75 base.checkForEmpty = function (blur) {
76 if (base.$field.val() === "") {
78 base.setOpacity(blur ? 1.0 : base.options.fadeOpacity);
84 base.prepForShow = function (e) {
86 // Prepare for a animate in...
87 base.$label.css({opacity: 0.0}).show();
89 // Reattach the keydown event
90 base.$field.bind('keydown.infieldlabel', function (e) {
96 base.hideOnChange = function (e) {
98 (e.keyCode === 16) || // Skip Shift
99 (e.keyCode === 9) // Skip Tab
106 base.showing = false;
109 // Remove keydown event to save on CPU processing
110 base.$field.unbind('keydown.infieldlabel');
113 // Run the initialization method
117 $.InFieldLabels.defaultOptions = {
118 fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
119 fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
123 $.fn.inFieldLabels = function (options) {
124 return this.each(function () {
125 // Find input or textarea based on for= attribute
126 // The for attribute on the label must contain the ID
127 // of the input or textarea element
128 var for_attr = $(this).attr('for'), $field;
130 return; // Nothing to attach, since the for field wasn't used
133 // Find the referenced input or textarea element
135 "input#" + for_attr + "[type='text']," +
136 "input#" + for_attr + "[type='search']," +
137 "input#" + for_attr + "[type='tel']," +
138 "input#" + for_attr + "[type='url']," +
139 "input#" + for_attr + "[type='email']," +
140 "input#" + for_attr + "[type='password']," +
141 "textarea#" + for_attr
144 if ($field.length === 0) {
145 return; // Again, nothing to attach
148 // Only create object for input[text], input[password], or textarea
149 (new $.InFieldLabels(this, $field[0], options));