-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.placeholder.js
159 lines (124 loc) · 4.07 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(function($) {
$.extend({
placeholder : {
settings : {
focusClass: 'placeholderFocus',
activeClass: 'placeholder',
overrideSupport: false,
preventRefreshIssues: true
},
debug : false,
log : function(msg){
if(!$.placeholder.debug) return;
msg = "[Placeholder] " + msg;
$.placeholder.hasFirebug ?
console.log(msg) :
$.placeholder.hasConsoleLog ?
window.console.log(msg) :
alert(msg);
},
hasFirebug : "console" in window && "firebug" in window.console,
hasConsoleLog: "console" in window && "log" in window.console
}
});
// check browser support for placeholder
$.support.placeholder = 'placeholder' in document.createElement('input');
// Replace the val function to never return placeholders
$.fn.plVal = $.fn.val;
$.fn.val = function(value) {
$.placeholder.log('in val');
if (this[0]) {
$.placeholder.log('have found an element');
var el = $(this[0]);
if (value != undefined) {
$.placeholder.log('in setter');
var currentValue = el.plVal(),
returnValue = $(this).plVal(value);
if (el.hasClass($.placeholder.settings.activeClass) && currentValue == el.attr('placeholder')) {
el.removeClass($.placeholder.settings.activeClass);
}
return returnValue;
}
if (el.hasClass($.placeholder.settings.activeClass) && el.plVal() == el.attr('placeholder')) {
$.placeholder.log('returning empty because it\'s a placeholder');
return '';
} else {
$.placeholder.log('returning original val');
return el.plVal();
}
}
$.placeholder.log('returning undefined');
return undefined;
};
// Clear placeholder values upon page reload
$(window).bind('beforeunload.placeholder', function() {
var els = $('input.' + $.placeholder.settings.activeClass);
if (els.length > 0) {
els.val('').attr('autocomplete', 'off');
}
});
// plugin code
$.fn.placeholder = function(opts) {
opts = $.extend({},$.placeholder.settings, opts);
// we don't have to do anything if the browser supports placeholder
if(!opts.overrideSupport && $.support.placeholder) {
return this;
}
var resetCursorPosition = function(){
if (this.setSelectionRange) {
this.setSelectionRange(0,0);
} else {
var range = this.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', 0);
range.select();
}
};
return this.each(function() {
var $el = $(this);
// skip if we do not have the placeholder attribute or input is of the password type
if (!$el.is('[placeholder]') || $el.is(':password')) {
return;
}
// Prevent values from being reapplied on refresh
if (opts.preventRefreshIssues) {
$el.attr('autocomplete','off');
}
$el.bind('focus.placeholder', function() {
var $el = $(this);
if ((this.value == $el.attr('placeholder') || this.value == "")) {
$el.val($el.attr('placeholder')).removeClass(opts.focusClass).addClass(opts.activeClass);
} else if ($el.hasClass('error')) {
// add this class so text will be cleared on keypress after error
// NOTE: login.js also adds this class on error so keypress will clear on any error that is not just date-related
$el.addClass('toClear');
}
resetCursorPosition.call(this);
});
$el.bind('keydown.placeholder',function(e) {
var $el = $(this);
if (this.value == $el.attr('placeholder') || this.value == "") {
$el.val('').removeClass(opts.activeClass).addClass(opts.focusClass);
} else if ($el.hasClass('toClear')) {
$el.val('').removeClass('toClear');
}
});
$el.bind('blur.placeholder', function() {
var $el = $(this);
$el.removeClass(opts.focusClass);
if (this.value == '') {
$el.val($el.attr('placeholder')).addClass(opts.activeClass);
}
});
$el.bind('mousedown.placeholder',function(){
resetCursorPosition.call(this);
});
$el.triggerHandler('blur');
// Prevent incorrect form values being posted
$el.parents('form').submit(function() {
$el.triggerHandler('focus.placeholder');
});
});
};
})(jQuery);