Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 1.41 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * jQuery placeholder Plugin
  3.  * version: 0.1
  4.  * @requires: jQuery v1.4.1 or later, Modernizr, and jquery livequery plugin
  5.  *
  6.  * http://docs.jquery.com/Plugins/livequery
  7.  *
  8.  * To use: When you write your input tags, include a title and placeholder attribute as
  9.  * well as a placeholder class. Also include styling for the hasPlaceholder class.
  10.  *
  11.  * E.g.:
  12.  * CSS:
  13.  * .hasPlaceholder { color: #ddd; font-style: italic; }
  14.  *
  15.  * <input type="text" class="placeholder" title="Username" placeholder="Username" name="username">
  16.  */
  17.  if (!Modernizr.input.placeholder) {
  18.   $(function() {
  19.  
  20.     var blurInputs = function (selector) {
  21.       selector.each(function () {
  22.         var trimmedTitle = $.trim(this.title), trimmedValue = $.trim(this.value);
  23.         if (trimmedTitle !== '' && (trimmedValue === '' || trimmedValue == trimmedTitle)) {
  24.           this.value = this.title;
  25.           $(this).addClass('hasPlaceholder');
  26.         }
  27.       });
  28.     };
  29.  
  30.     $(':text.placeholder')
  31.       .livequery(function () {
  32.         blurInputs($(this));
  33.       })
  34.       .live('focus', function () {
  35.         if ($.trim(this.title) !== '' && $.trim(this.value) === $.trim(this.title)) {
  36.           this.value = '';
  37.           $(this).removeClass('hasPlaceholder');
  38.         }
  39.       })
  40.       .live('blur', function () {
  41.         blurInputs($(this));
  42.       });
  43.  
  44.     $("form").live("submit", function () {
  45.       $(this).find(':text.hasPlaceholder').val('');
  46.     });
  47.   });
  48. }
clone this paste RAW Paste Data