|
1 |
| -/*! http://mths.be/placeholder v1.8.7 by @mathias */ |
| 1 | +/*! http://mths.be/placeholder v2.0.0 by @mathias */ |
2 | 2 | ;(function(window, document, $) {
|
3 | 3 |
|
4 | 4 | var isInputSupported = 'placeholder' in document.createElement('input'),
|
5 | 5 | isTextareaSupported = 'placeholder' in document.createElement('textarea'),
|
6 | 6 | prototype = $.fn,
|
| 7 | + valHooks = $.valHooks, |
| 8 | + hooks, |
7 | 9 | placeholder;
|
8 | 10 |
|
9 | 11 | if (isInputSupported && isTextareaSupported) {
|
|
20 | 22 | return this
|
21 | 23 | .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
|
22 | 24 | .not('.placeholder')
|
23 |
| - .bind('focus.placeholder', clearPlaceholder) |
24 |
| - .bind('blur.placeholder', setPlaceholder) |
| 25 | + .bind({ |
| 26 | + 'focus.placeholder': clearPlaceholder, |
| 27 | + 'blur.placeholder': setPlaceholder |
| 28 | + }) |
25 | 29 | .trigger('blur.placeholder').end();
|
26 | 30 | };
|
27 | 31 |
|
28 | 32 | placeholder.input = isInputSupported;
|
29 | 33 | placeholder.textarea = isTextareaSupported;
|
30 | 34 |
|
| 35 | + hooks = { |
| 36 | + 'get': function(element) { |
| 37 | + var $element = $(element); |
| 38 | + return $element.hasClass('placeholder') ? '' : element.value; |
| 39 | + }, |
| 40 | + 'set': function(element, value) { |
| 41 | + var $element = $(element); |
| 42 | + if (value == '') { |
| 43 | + element.value = value; |
| 44 | + // We can’t use `triggerHandler` here because of dummy text/password inputs :( |
| 45 | + setPlaceholder.call(element); |
| 46 | + } else if ($element.hasClass('placeholder')) { |
| 47 | + clearPlaceholder.call(element, true, value) || (element.value = value); |
| 48 | + } else { |
| 49 | + element.value = value; |
| 50 | + } |
| 51 | + // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363 |
| 52 | + return $element; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + isInputSupported || (valHooks.input = hooks); |
| 57 | + isTextareaSupported || (valHooks.textarea = hooks); |
| 58 | + |
31 | 59 | $(function() {
|
32 | 60 | // Look for forms
|
33 | 61 | $(document).delegate('form', 'submit.placeholder', function() {
|
|
41 | 69 |
|
42 | 70 | // Clear placeholder values upon page reload
|
43 | 71 | $(window).bind('unload.placeholder', function() {
|
44 |
| - $('.placeholder').val(''); |
| 72 | + $('.placeholder').each(function() { |
| 73 | + this.value = ''; |
| 74 | + }); |
45 | 75 | });
|
46 | 76 |
|
47 | 77 | }
|
|
58 | 88 | return newAttrs;
|
59 | 89 | }
|
60 | 90 |
|
61 |
| - function clearPlaceholder() { |
62 |
| - var $input = $(this); |
63 |
| - if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) { |
| 91 | + function clearPlaceholder(event, value) { |
| 92 | + var input = this, |
| 93 | + $input = $(input); |
| 94 | + if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) { |
64 | 95 | if ($input.data('placeholder-password')) {
|
65 |
| - $input.hide().next().show().focus().attr('id', $input.removeAttr('id').data('placeholder-id')); |
| 96 | + $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id')); |
| 97 | + // If `clearPlaceholder` was called from `$.valHooks.input.set` |
| 98 | + if (event === true) { |
| 99 | + return $input[0].value = value; |
| 100 | + } |
| 101 | + $input.focus(); |
66 | 102 | } else {
|
67 |
| - $input.val('').removeClass('placeholder'); |
| 103 | + input.value = ''; |
| 104 | + $input.removeClass('placeholder'); |
68 | 105 | }
|
69 | 106 | }
|
70 | 107 | }
|
71 | 108 |
|
72 | 109 | function setPlaceholder() {
|
73 | 110 | var $replacement,
|
74 |
| - $input = $(this), |
| 111 | + input = this, |
| 112 | + $input = $(input), |
75 | 113 | $origInput = $input,
|
76 | 114 | id = this.id;
|
77 |
| - if ($input.val() === '') { |
78 |
| - if ($input.is(':password')) { |
| 115 | + if (input.value == '') { |
| 116 | + if (input.type == 'password') { |
79 | 117 | if (!$input.data('placeholder-textinput')) {
|
80 | 118 | try {
|
81 | 119 | $replacement = $input.clone().attr({ 'type': 'text' });
|
|
84 | 122 | }
|
85 | 123 | $replacement
|
86 | 124 | .removeAttr('name')
|
87 |
| - // We could just use the `.data(obj)` syntax here, but that wouldn’t work in pre-1.4.3 jQueries |
88 |
| - .data('placeholder-password', true) |
89 |
| - .data('placeholder-id', id) |
| 125 | + .data({ |
| 126 | + 'placeholder-password': true, |
| 127 | + 'placeholder-id': id |
| 128 | + }) |
90 | 129 | .bind('focus.placeholder', clearPlaceholder);
|
91 | 130 | $input
|
92 |
| - .data('placeholder-textinput', $replacement) |
93 |
| - .data('placeholder-id', id) |
| 131 | + .data({ |
| 132 | + 'placeholder-textinput': $replacement, |
| 133 | + 'placeholder-id': id |
| 134 | + }) |
94 | 135 | .before($replacement);
|
95 | 136 | }
|
96 | 137 | $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
|
| 138 | + // Note: `$input[0] != input` now! |
97 | 139 | }
|
98 |
| - $input.addClass('placeholder').val($input.attr('placeholder')); |
| 140 | + $input.addClass('placeholder'); |
| 141 | + $input[0].value = $input.attr('placeholder'); |
99 | 142 | } else {
|
100 | 143 | $input.removeClass('placeholder');
|
101 | 144 | }
|
|
0 commit comments