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);
52 .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
54 return !$(this).data('placeholder-enabled');
57 'focus.placeholder drop.placeholder': clearPlaceholder,
58 'blur.placeholder': setPlaceholder
60 .data('placeholder-enabled', true)
61 .trigger('blur.placeholder');
65 placeholder.input = isInputSupported;
66 placeholder.textarea = isTextareaSupported;
69 'get': function(element) {
70 var $element = $(element),
71 $passwordInput = $element.data('placeholder-password');
73 return $passwordInput[0].value;
76 return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;
78 'set': function(element, value) {
79 var $element = $(element),
80 $passwordInput = $element.data('placeholder-password');
82 $passwordInput[0].value = value;
86 if (!$element.data('placeholder-enabled')) {
87 element.value = value;
91 element.value = value;
92 // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
93 if (element !== safeActiveElement()) {
94 // We can't use `triggerHandler` here because of dummy text/password inputs :(
95 setPlaceholder.call(element);
97 } else if ($element.hasClass('placeholder')) {
98 if(!clearPlaceholder.call(element, true, value)) {
99 element.value = value;
102 element.value = value;
104 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
109 if (!isInputSupported) {
110 valHooks.input = hooks;
111 propHooks.value = hooks;
113 if (!isTextareaSupported) {
114 valHooks.textarea = hooks;
115 propHooks.value = hooks;
120 $(document).delegate('form', 'submit.placeholder', function() {
121 // Clear the placeholder values so they don't get submitted
122 var $inputs = $('.placeholder', this).each(clearPlaceholder);
123 setTimeout(function() {
124 $inputs.each(setPlaceholder);
129 // Clear placeholder values upon page reload
130 $(window).bind('beforeunload.placeholder', function() {
131 $('.placeholder').each(function() {
138 function args(elem) {
139 // Return an object of element attributes
141 rinlinejQuery = /^jQuery\d+$/;
142 $.each(elem.attributes, function(i, attr) {
143 if (attr.specified && !rinlinejQuery.test(attr.name)) {
144 newAttrs[attr.name] = attr.value;
150 function clearPlaceholder(event, value) {
153 if (input.value === $input.attr('placeholder') && $input.hasClass('placeholder')) {
154 if ($input.data('placeholder-password')) {
155 $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
156 // If `clearPlaceholder` was called from `$.valHooks.input.set`
157 if (event === true) {
158 $input[0].value = value;
164 $input.removeClass('placeholder');
165 if(input === safeActiveElement()) {
172 function setPlaceholder() {
178 if (input.type === 'password') {
179 if (!$input.data('placeholder-textinput')) {
181 $replacement = $input.clone().attr({ 'type': 'text' });
183 $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
188 'placeholder-password': $input,
191 .bind('focus.placeholder drop.placeholder', clearPlaceholder);
194 'placeholder-textinput': $replacement,
197 .before($replacement);
199 $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
200 // Note: `$input[0] != input` now!
202 $input.addClass('placeholder');
203 $input[0].value = $input.attr('placeholder');
205 $input.removeClass('placeholder');
209 function safeActiveElement() {
210 // Avoid IE9 `document.activeElement` of death
211 // https://github.com/mathiasbynens/jquery-placeholder/pull/99
213 return document.activeElement;
217 function changePlaceholder(text) {
218 var hasArgs = arguments.length,
221 if($input.attr('placeholder') !== text) {
222 $input.prop('placeholder', text);
223 if($input.hasClass('placeholder')) {
224 $input[0].value = text;