2 * In-Field Label jQuery Plugin
3 * http://fuelyourcoding.com/scripts/infield.html
5 * Copyright (c) 2009 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() != ""){
38 base.$field.focus(function(){
41 base.checkForEmpty(true);
42 }).bind('keydown.infieldlabel',function(e){
43 // Use of a namespace (.infieldlabel) allows us to
44 // unbind just this method later
46 }).change(function(e){
48 }).bind('onPropertyChange', function(){
53 // If the label is currently showing
54 // then fade it down to the amount
55 // specified in the settings
56 base.fadeOnFocus = function(){
58 base.setOpacity(base.options.fadeOpacity);
62 base.setOpacity = function(opacity){
63 base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
64 base.showing = (opacity > 0.0);
67 // Checks for empty as a fail safe
68 // set blur to true when passing from
70 base.checkForEmpty = function(blur){
71 if(base.$field.val() == ""){
73 base.setOpacity( blur ? 1.0 : base.options.fadeOpacity );
79 base.prepForShow = function(e){
81 // Prepare for a animate in...
82 base.$label.css({opacity: 0.0}).show();
84 // Reattach the keydown event
85 base.$field.bind('keydown.infieldlabel',function(e){
91 base.hideOnChange = function(e){
93 (e.keyCode == 16) || // Skip Shift
94 (e.keyCode == 9) // Skip Tab
102 // Remove keydown event to save on CPU processing
103 base.$field.unbind('keydown.infieldlabel');
105 // ff3.5 fix, forces key to be written in input field
109 // Run the initialization method
113 $.InFieldLabels.defaultOptions = {
114 fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
115 fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
119 $.fn.inFieldLabels = function(options){
120 return this.each(function(){
121 // Find input or textarea based on for= attribute
122 // The for attribute on the label must contain the ID
123 // of the input or textarea element
124 var for_attr = $(this).attr('for');
125 if( !for_attr ) return; // Nothing to attach, since the for field wasn't used
128 // Find the referenced input or textarea element
130 "input#" + for_attr + "[type='text']," +
131 "input#" + for_attr + "[type='password']," +
132 "textarea#" + for_attr
135 if( $field.length == 0) return; // Again, nothing to attach
137 // Only create object for input[text], input[password], or textarea
138 (new $.InFieldLabels(this, $field[0], options));