/
jquery.floatLabel.js
47 lines (43 loc) · 1.21 KB
/
jquery.floatLabel.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
/*
* February 2015
* floatLabel 1.0.1
* @author Mario Vidov
* @url http://vidov.it
* @twitter MarioVidov
* GPL license
*/
$.fn.floatLabel = function(options) {
var settings = $.extend({
}, options);
var isFocused = "active",
isVisible = "visible";
this.init = function() {
var inputs = $(this.selector);
$.each(inputs, function(i, el) {
var hasLabel = $(el).parent().find("label").length;
if (!hasLabel) {
$(el).parent().prepend("<label />");
}
});
};
$(this.selector).bind("isEmpty",function(){
var label = $(this).prev("label");
var name = $(this).attr("placeholder") || $(this).attr("name") || $(this).attr("id") || "";
label.text(name);
label.attr({
for: this.id
});
if(this.value !== ""){
label.addClass(isVisible);
} else {
label.removeClass(isVisible);
}
}).on("keyup",function(){
$(this).trigger("isEmpty");
}).on("focus",function(){
$(this).prev("label").addClass(isFocused);
}).on("blur",function(){
$(this).prev("label").removeClass(isFocused);
});
this.init();
};