-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.placeholding.plugin.js
74 lines (66 loc) · 1.75 KB
/
jquery.placeholding.plugin.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
/**
* PlaceHolding
*
* @description mimic placeholder
* @author [email protected]
*
* @require jQuery 1.7 or later
* @usage $('form').placeholding();
*/
(function($){
$.fn.extend({
placeholding: function() {
if ((function(){ return ('placeholder' in document.createElement('input')); })() === true){
return false;
}
var STATUS = { ON: 1, OFF: 0 };
//
function whenBlur($elmt){
if ($elmt.val() == ''){
$elmt.data('originalcolor', $elmt.css('color'));
$elmt.css('color', 'rgb(153, 153, 153)');
$elmt.val($elmt.attr('placeholder'));
$elmt.data('placeholdingstatus', STATUS.ON);
}else{
$elmt.data('placeholdingstatus', STATUS.OFF);
}
}
//
function whenFocus($elmt){
if ($elmt.data('placeholdingstatus') === STATUS.ON){
$elmt.val('');
$elmt.css('color', $elmt.data('originalcolor'));
$elmt.data('placeholdingstatus', STATUS.OFF);
}
}
return this.each(function(){
// allow only form tag
if (this.tagName.toLowerCase() !== 'form'){
return false;
}
// prevent duplicating
if ($(this).data('placeholdingapplied') === '1'){
return false;
}
$(this).data('placeholdingapplied', '1');
// bind event handler to input & textarea elements
$(this).on({
'focus': function(){
whenFocus($(this));
},
'blur': function(){
whenBlur($(this));
}
}, 'input[placeholder], textarea[placeholder]');
// bind submit handler
$(this).on('submit', function(){
$('input[placeholder], textarea[placeholder]', this).each(function(i, e){
whenFocus($(e));
});
});
// init (triggering blur event)
$('input[placeholder], textarea[placeholder]', this).trigger('blur');
});
}
});
})(jQuery);