2 * HTML5 placeholder emulation for jQuery plugin
4 * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
6 * This is a fork from Mathias Bynens' jquery.placeholder as of this commit
7 * https://github.com/mathiasbynens/jquery-placeholder/blob/47f05d400e2dd16b59d144141a2cf54a9a77c502/jquery.placeholder.js
9 * @author Mathias Bynens <http://mathiasbynens.be/>
10 * @author Trevor Parscal <tparscal@wikimedia.org>, 2012
11 * @author Krinkle <krinklemail@gmail.com>, 2012
12 * @author Alex Ivanov <alexivanov97@gmail.com>, 2013
18 var isInputSupported
= 'placeholder' in document
.createElement('input'),
19 isTextareaSupported
= 'placeholder' in document
.createElement('textarea'),
21 valHooks
= $.valHooks
,
22 propHooks
= $.propHooks
,
26 if (isInputSupported
&& isTextareaSupported
) {
28 placeholder
= prototype.placeholder = function (text
) {
29 var hasArgs
= arguments
.length
;
32 changePlaceholder
.call(this, text
);
38 placeholder
.input
= placeholder
.textarea
= true;
42 placeholder
= prototype.placeholder = function (text
) {
44 hasArgs
= arguments
.length
;
47 changePlaceholder
.call(this, text
);
51 .filter((isInputSupported
? 'textarea' : ':input') + '[placeholder]')
53 return !$(this).data('placeholder-enabled');
56 'focus.placeholder drop.placeholder': clearPlaceholder
,
57 'blur.placeholder': setPlaceholder
59 .data('placeholder-enabled', true)
60 .trigger('blur.placeholder');
64 placeholder
.input
= isInputSupported
;
65 placeholder
.textarea
= isTextareaSupported
;
68 'get': function (element
) {
69 var $element
= $(element
),
70 $passwordInput
= $element
.data('placeholder-password');
72 return $passwordInput
[0].value
;
75 return $element
.data('placeholder-enabled') && $element
.hasClass('placeholder') ? '' : element
.value
;
77 'set': function (element
, value
) {
78 var $element
= $(element
),
79 $passwordInput
= $element
.data('placeholder-password');
81 $passwordInput
[0].value
= value
;
85 if (!$element
.data('placeholder-enabled')) {
86 element
.value
= value
;
90 element
.value
= value
;
91 // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
92 if (element
!== safeActiveElement()) {
93 // We can't use `triggerHandler` here because of dummy text/password inputs :(
94 setPlaceholder
.call(element
);
96 } else if ($element
.hasClass('placeholder')) {
97 if (!clearPlaceholder
.call(element
, true, value
)) {
98 element
.value
= value
;
101 element
.value
= value
;
103 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
108 if (!isInputSupported
) {
109 valHooks
.input
= hooks
;
110 propHooks
.value
= hooks
;
112 if (!isTextareaSupported
) {
113 valHooks
.textarea
= hooks
;
114 propHooks
.value
= hooks
;
119 $(document
).delegate('form', 'submit.placeholder', function () {
120 // Clear the placeholder values so they don't get submitted
121 var $inputs
= $('.placeholder', this).each(clearPlaceholder
);
122 setTimeout(function () {
123 $inputs
.each(setPlaceholder
);
128 // Clear placeholder values upon page reload
129 $(window
).bind('beforeunload.placeholder', function () {
130 $('.placeholder').each(function () {
137 function args(elem
) {
138 // Return an object of element attributes
140 rinlinejQuery
= /^jQuery\d+$/;
141 $.each(elem
.attributes
, function (i
, attr
) {
142 if (attr
.specified
&& !rinlinejQuery
.test(attr
.name
)) {
143 newAttrs
[attr
.name
] = attr
.value
;
149 function clearPlaceholder(event
, value
) {
152 if (input
.value
=== $input
.attr('placeholder') && $input
.hasClass('placeholder')) {
153 if ($input
.data('placeholder-password')) {
154 $input
= $input
.hide().next().show().attr('id', $input
.removeAttr('id').data('placeholder-id'));
155 // If `clearPlaceholder` was called from `$.valHooks.input.set`
156 if (event
=== true) {
157 $input
[0].value
= value
;
163 $input
.removeClass('placeholder');
164 if (input
=== safeActiveElement()) {
171 function setPlaceholder() {
177 if (input
.type
=== 'password') {
178 if (!$input
.data('placeholder-textinput')) {
180 $replacement
= $input
.clone().attr({ 'type': 'text' });
182 $replacement
= $('<input>').attr($.extend(args(this), { 'type': 'text' }));
187 'placeholder-password': $input
,
190 .bind('focus.placeholder drop.placeholder', clearPlaceholder
);
193 'placeholder-textinput': $replacement
,
196 .before($replacement
);
198 $input
= $input
.removeAttr('id').hide().prev().attr('id', id
).show();
199 // Note: `$input[0] != input` now!
201 $input
.addClass('placeholder');
202 $input
[0].value
= $input
.attr('placeholder');
204 $input
.removeClass('placeholder');
208 function safeActiveElement() {
209 // Avoid IE9 `document.activeElement` of death
210 // https://github.com/mathiasbynens/jquery-placeholder/pull/99
212 return document
.activeElement
;
216 function changePlaceholder(text
) {
217 var hasArgs
= arguments
.length
,
220 if ($input
.attr('placeholder') !== text
) {
221 $input
.prop('placeholder', text
);
222 if ($input
.hasClass('placeholder')) {
223 $input
[0].value
= text
;