-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjquery.infieldLabel.js
94 lines (71 loc) · 1.96 KB
/
jquery.infieldLabel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function($) {
$.infieldLabel = function(el, options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
// internal variables
base.$input = null;
base.init = function() {
base.options = $.extend({}, $.infieldLabel.defaultOptions, options);
base.setup();
};
/*
* set up
*/
// first time input setup
base.setup = function() {
base.$input = base.$el.find('input, textarea');
base.$label = base.$el.find('label');
base.$el.addClass('init');
// hide label if there's already a value
base.blur();
// bind events
base.bind();
};
// binds the focus, blur and change events
base.bind = function() {
base.$input
.on('focus.infield', function() {
base.$el
.removeClass(base.options.hideClass)
.addClass(base.options.focusClass);
}).on('blur.infield change.infield', function() {
base.blur();
});
base.$label.on('click.infield', function() {
base.$el
.removeClass(base.options.hideClass)
.addClass(base.options.focusClass);
base.$input.focus();
});
};
base.blur = function() {
if (base.$input.val() !== '') {
base.$el
.removeClass(base.options.focusClass)
.addClass(base.options.hideClass);
} else {
base.$el
.removeClass(base.options.focusClass + ' ' + base.options.hideClass);
}
};
/*
* initialize
*/
base.init();
};
/*
* options
*/
$.infieldLabel.defaultOptions = {
focusClass: 'placeholder-focus',
hideClass: 'placeholder-hide'
};
$.fn.infieldLabel = function(options) {
this.each(function() {
new $.infieldLabel(this, options);
});
};
})(jQuery);