jquery.placeholder.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*! http://mths.be/placeholder v2.0.7 by @mathias */
  2. ;(function(window, document, $) {
  3. var isInputSupported = 'placeholder' in document.createElement('input');
  4. var isTextareaSupported = 'placeholder' in document.createElement('textarea');
  5. var prototype = $.fn;
  6. var valHooks = $.valHooks;
  7. var propHooks = $.propHooks;
  8. var hooks;
  9. var placeholder;
  10. if (isInputSupported && isTextareaSupported) {
  11. placeholder = prototype.placeholder = function() {
  12. return this;
  13. };
  14. placeholder.input = placeholder.textarea = true;
  15. } else {
  16. placeholder = prototype.placeholder = function() {
  17. var $this = this;
  18. $this
  19. .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
  20. .not('.placeholder')
  21. .bind({
  22. 'focus.placeholder': clearPlaceholder,
  23. 'blur.placeholder': setPlaceholder
  24. })
  25. .data('placeholder-enabled', true)
  26. .trigger('blur.placeholder');
  27. return $this;
  28. };
  29. placeholder.input = isInputSupported;
  30. placeholder.textarea = isTextareaSupported;
  31. hooks = {
  32. 'get': function(element) {
  33. var $element = $(element);
  34. var $passwordInput = $element.data('placeholder-password');
  35. if ($passwordInput) {
  36. return $passwordInput[0].value;
  37. }
  38. return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;
  39. },
  40. 'set': function(element, value) {
  41. var $element = $(element);
  42. var $passwordInput = $element.data('placeholder-password');
  43. if ($passwordInput) {
  44. return $passwordInput[0].value = value;
  45. }
  46. if (!$element.data('placeholder-enabled')) {
  47. return element.value = value;
  48. }
  49. if (value == '') {
  50. element.value = value;
  51. // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
  52. if (element != safeActiveElement()) {
  53. // We can't use `triggerHandler` here because of dummy text/password inputs :(
  54. setPlaceholder.call(element);
  55. }
  56. } else if ($element.hasClass('placeholder')) {
  57. clearPlaceholder.call(element, true, value) || (element.value = value);
  58. } else {
  59. element.value = value;
  60. }
  61. // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
  62. return $element;
  63. }
  64. };
  65. if (!isInputSupported) {
  66. valHooks.input = hooks;
  67. propHooks.value = hooks;
  68. }
  69. if (!isTextareaSupported) {
  70. valHooks.textarea = hooks;
  71. propHooks.value = hooks;
  72. }
  73. $(function() {
  74. // Look for forms
  75. $(document).delegate('form', 'submit.placeholder', function() {
  76. // Clear the placeholder values so they don't get submitted
  77. var $inputs = $('.placeholder', this).each(clearPlaceholder);
  78. setTimeout(function() {
  79. $inputs.each(setPlaceholder);
  80. }, 10);
  81. });
  82. });
  83. // Clear placeholder values upon page reload
  84. $(window).bind('beforeunload.placeholder', function() {
  85. $('.placeholder').each(function() {
  86. this.value = '';
  87. });
  88. });
  89. }
  90. function args(elem) {
  91. // Return an object of element attributes
  92. var newAttrs = {};
  93. var rinlinejQuery = /^jQuery\d+$/;
  94. $.each(elem.attributes, function(i, attr) {
  95. if (attr.specified && !rinlinejQuery.test(attr.name)) {
  96. newAttrs[attr.name] = attr.value;
  97. }
  98. });
  99. return newAttrs;
  100. }
  101. function clearPlaceholder(event, value) {
  102. var input = this;
  103. var $input = $(input);
  104. if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) {
  105. if ($input.data('placeholder-password')) {
  106. $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
  107. // If `clearPlaceholder` was called from `$.valHooks.input.set`
  108. if (event === true) {
  109. return $input[0].value = value;
  110. }
  111. $input.focus();
  112. } else {
  113. input.value = '';
  114. $input.removeClass('placeholder');
  115. input == safeActiveElement() && input.select();
  116. }
  117. }
  118. }
  119. function setPlaceholder() {
  120. var $replacement;
  121. var input = this;
  122. var $input = $(input);
  123. var id = this.id;
  124. if (input.value == '') {
  125. if (input.type == 'password') {
  126. if (!$input.data('placeholder-textinput')) {
  127. try {
  128. $replacement = $input.clone().attr({ 'type': 'text' });
  129. } catch(e) {
  130. $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
  131. }
  132. $replacement
  133. .removeAttr('name')
  134. .data({
  135. 'placeholder-password': $input,
  136. 'placeholder-id': id
  137. })
  138. .bind('focus.placeholder', clearPlaceholder);
  139. $input
  140. .data({
  141. 'placeholder-textinput': $replacement,
  142. 'placeholder-id': id
  143. })
  144. .before($replacement);
  145. }
  146. $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
  147. // Note: `$input[0] != input` now!
  148. }
  149. $input.addClass('placeholder');
  150. $input[0].value = $input.attr('placeholder');
  151. } else {
  152. $input.removeClass('placeholder');
  153. }
  154. }
  155. function safeActiveElement() {
  156. // Avoid IE9 `document.activeElement` of death
  157. // https://github.com/mathiasbynens/jquery-placeholder/pull/99
  158. try {
  159. return document.activeElement;
  160. } catch (err) {}
  161. }
  162. }(this, document, jQuery));
  163. $(function(){
  164. $("[placeholder]").placeholder();
  165. });