-
Notifications
You must be signed in to change notification settings - Fork 13
/
jquery.placeholder.js
55 lines (49 loc) · 1.32 KB
/
jquery.placeholder.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
/**
jQuery placeholder plugin.
Makes browsers that does not support HTML5 placeholder attribute, preform as a modern browser.
Created by:
Lasse Søberg
06.04.2011
**/
(function($) {
var native_support = ('placeholder' in document.createElement('input'));
$.fn.placeholder = function(command) {
if(!native_support) {
if(command) {
switch(command) {
case 'clear':
this.each(function() {
var el = $(this)
if(el.data('isEmpty') || el.val() == el.attr('placeholder')) {
el.val('');
}
});
break;
}
return this;
}
this.each(function() {
if(!$(this).data('gotPlaceholder')) {
$(this).focus(function() {
var el = $(this);
if(el.data('isEmpty')) {
el.val('').removeClass('placeholder');
}
}).blur(function() {
var el = $(this);
if(el.data('isEmpty') || !el.val().length) {
el.val(el.attr('placeholder')).addClass('placeholder');
}
}).keyup(function() {
var el = $(this);
el.data('isEmpty', (el.val().length == 0));
}).data('gotPlaceholder', true);
if(!$(this).val().length || $(this).val() == $(this).attr('placeholder')) {
$(this).val($(this).attr('placeholder')).addClass('placeholder').data('isEmpty', true);
}
}
});
}
return this;
}
})(jQuery);