jquery.placeholder-1.1.9.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*****************************************************************************
  2. jQuery Placeholder 1.1.9 (with minor modifications for CardDavMATE & CalDavZAP)
  3. Copyright (c) 2010 Michael J. Ryan (http://tracker1.info/)
  4. Dual licensed under the MIT and GPL licenses:
  5. http://www.opensource.org/licenses/mit-license.php
  6. http://www.gnu.org/licenses/gpl.html
  7. ------------------------------------------------------------------------------
  8. Sets up a watermark for inputted fields... this will create a LABEL.watermark
  9. tag immediately following the input tag, the positioning will be set absolute,
  10. and it will be positioned to match the input tag.
  11. To activate:
  12. $('input[placeholder],textarea[placeholder]').placeholder();
  13. NOTE, when changing a value via script:
  14. $('#input_id').val('new value').change(); //force change event, so placeholder sets properly
  15. To style the tags as appropriate (you'll want to make sure the font matches):
  16. label.placeholder {
  17. cursor: text; <--- display a cursor to match the text input
  18. padding: 4px 4px 4px 4px; <--- this should match the border+padding
  19. for the input field(s)
  20. color: #999999; <--- this will display as faded
  21. }
  22. You'll also want to have the color set for browsers with native support
  23. input:placeholder, textarea:placeholder {
  24. color: #999999;
  25. }
  26. input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
  27. color: #999999;
  28. }
  29. ------------------------------------------------------------------------------
  30. Thanks to...
  31. http://www.alistapart.com/articles/makingcompactformsmoreaccessible
  32. http://plugins.jquery.com/project/overlabel
  33. This works similar to the overlabel, but creates the actual label tag
  34. based on the placeholder attribute on the input tag, instead of
  35. relying on the markup to provide it.
  36. *****************************************************************************/
  37. (function($){
  38. var ph = "PLACEHOLDER-INPUT";
  39. var phl = "PLACEHOLDER-LABEL";
  40. var boundEvents = false;
  41. var default_options = {
  42. labelClass: 'placeholder'
  43. };
  44. //check for native support for placeholder attribute, if so stub methods and return
  45. var input = document.createElement("input");
  46. if ('placeholder' in input) {
  47. $.fn.placeholder = $.fn.unplaceholder = function(){}; //empty function
  48. delete input; //cleanup IE memory
  49. return;
  50. };
  51. delete input;
  52. //bind to resize to fix placeholders when the page resizes (fields are hidden/displayed, which can change positioning).
  53. $(window).resize(checkResize);
  54. $.fn.placeholder = function(options) {
  55. bindEvents();
  56. var opts = $.extend(default_options, options)
  57. this.each(function(){
  58. var rnd=Math.random().toString(32).replace(/\./,'')
  59. ,input=$(this)
  60. ,label=$('<label style="position:absolute;display:none;top:0;left:0;"></label>');
  61. if (!input.attr('placeholder') || input.data(ph) === ph) return; //already watermarked
  62. //make sure the input tag has an ID assigned, if not, assign one.
  63. if (!input.attr('id')) input.attr('id', 'input_' + rnd);
  64. label .attr('id',input.attr('id') + "_placeholder")
  65. .data(ph, '#' + input.attr('id')) //reference to the input tag
  66. .attr('for',input.attr('id'))
  67. .addClass(opts.labelClass)
  68. .addClass(opts.labelClass + '-for-' + this.tagName.toLowerCase()) //ex: watermark-for-textarea
  69. .addClass(phl)
  70. .text(input.attr('placeholder'));
  71. input
  72. .data(phl, '#' + label.attr('id')) //set a reference to the label
  73. .data(ph,ph) //set that the field is watermarked
  74. .addClass(ph) //add the watermark class
  75. .after(label) //add the label field to the page
  76. //setup overlay
  77. itemFocus.call(this);
  78. itemBlur.call(this);
  79. });
  80. };
  81. $.fn.unplaceholder = function(){
  82. this.each(function(){
  83. var input=$(this),
  84. label=$(input.data(phl));
  85. if (input.data(ph) !== ph) return;
  86. label.remove();
  87. input.removeData(ph).removeData(phl).removeClass(ph).unbind('change',itemChange);
  88. });
  89. };
  90. function bindEvents() {
  91. if (boundEvents) return;
  92. //prepare live bindings if not already done.
  93. $(document).on('reset', 'form', function(){$(this).find('.'+ph).each(itemBlur);});
  94. $(document).on('keydown mousedown mouseclick focus focusin', '.'+ph, itemFocus);
  95. $(document).on('blur focusout', '.'+ph, itemBlur);
  96. $(document).on('change', '.'+ph, itemChange);
  97. $(document).on('click mouseup', '.'+phl, function(){$($(this).data(ph)).focus();});
  98. bound = true;
  99. boundEvents = true;
  100. };
  101. function itemChange() {
  102. var input = $(this);
  103. if (!!input.val()) {
  104. $(input.data(phl)).hide();
  105. return;
  106. }
  107. if (input.data(ph+'FOCUSED') != 1) {
  108. showPHL(input);
  109. }
  110. }
  111. function itemFocus() {
  112. $($(this).data(ph+'FOCUSED',1).data(phl)).hide();
  113. };
  114. function itemBlur() {
  115. var that = this;
  116. showPHL($(this).removeData(ph+'FOCUSED'));
  117. //use timeout to let other validators/formatters directly bound to blur/focusout work
  118. setTimeout(function(){
  119. var input = $(that);
  120. //if the item wasn't refocused, test the item
  121. if (input.data(ph+'FOCUSED') != 1) {
  122. showPHL(input);
  123. }
  124. }, 200);
  125. };
  126. function showPHL(input, forced) {
  127. var label = $(input.data(phl));
  128. //if not already shown, and needs to be, show it.
  129. if ((forced || label.css('display') == 'none') && !input.val())
  130. label
  131. .text(input.attr('placeholder'))
  132. .css('top', input.position().top + 'px')
  133. .css('left', input.position().left + 'px')
  134. .css('display', 'block');
  135. //console.dir({ 'input': { 'id':input.attr('id'), 'pos': input.position() }});
  136. }
  137. var cr;
  138. function checkResize() {
  139. if (cr) window.clearTimeout(cr);
  140. cr = window.setTimeout(checkResize2, 50);
  141. }
  142. function checkResize2() {
  143. $('.' + ph).each(function(){
  144. var input = $(this);
  145. var focused = $(this).data(ph+'FOCUSED');
  146. if (!focused) showPHL(input, true);
  147. });
  148. }
  149. }(jQuery));