jquery.placeholder.js 4.4 KB

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