forked from m10l/FloatLabel.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.FloatLabel.js
52 lines (37 loc) · 1 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
48
49
50
51
52
(function( $ ){
$.fn.FloatLabel = function( options ){
var defaults = {
populatedClass : 'populated',
focusedClass : 'focused'
},
settings = $.extend({}, defaults, options);
return this.each(function(){
var element = $(this),
label = element.find('label'),
input = element.find('textarea, input');
if( input.val() == '' ) {
input.val( label.text() );
} else {
element.addClass( settings.populatedClass );
}
input.on( 'focus', function(){
element.addClass( settings.focusedClass );
if( input.val() === label.text() ){
input.val('');
} else {
element.addClass( settings.populatedClass );
}
});
input.on( 'blur', function(){
element.removeClass( settings.focusedClass );
if( !input.val() ){
input.val( label.text() );
element.removeClass( settings.populatedClass );
}
});
input.on( 'keyup', function(){
element.addClass( settings.populatedClass );
});
});
};
})( jQuery );