forked from ten1seven/infieldLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.infieldLabel.js
More file actions
82 lines (61 loc) · 1.86 KB
/
Copy pathjquery.infieldLabel.js
File metadata and controls
82 lines (61 loc) · 1.86 KB
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
/*! jquery.infieldLabel.js v 1.0 | Author: Jeremy Fields [jeremy.fields@viget.com], 2013 | License: MIT */
(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);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("infieldLabel", base);
// internal variables
base.$input = null;
base.init = function() {
base.options = $.extend({}, $.infieldLabel.defaultOptions, options);
base.setup();
};
// setup
// ==========================================================================
// first time input setup
base.setup = function() {
base.$input = base.$el.find("input[type=text],input[type=password]");
// 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.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);
}
};
// run initializer
// ==========================================================================
base.init();
};
$.infieldLabel.defaultOptions = {
focusClass: "placeholder-focus",
hideClass: "placeholder-hide"
};
$.fn.infieldLabel = function(options){
this.each(function(){
(new $.infieldLabel(this, options));
});
};
})(jQuery);