Don't like ads? PRO users don't see any ads ;-)
Guest

javascript default text replace

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 2.46 KB  |  hits: 24  |  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. var inject = {};
  3. (function($)
  4. {
  5.         //Atribui acoes aos objetos passados
  6.         //@author: Pedro Sampaio
  7.         inject =
  8.         {
  9.                 input :
  10.                 {
  11.                         textcounter: function(obj)
  12.                         {
  13.                                 if(!obj)
  14.                                         obj = $(this);
  15.  
  16.                                 obj.maxlength({
  17.                                         maxCharacters: obj.attr('data-length'),
  18.                                         status: true, // True to show status indicator bewlow the element
  19.                                         statusClass: "status", // The class on the status div
  20.                                         statusText: "caracteres restantes",
  21.                                         notificationClass: "cheio",
  22.                                         showAlert: false,
  23.                                         alertText: "Você já chegou no limite de caracteres.",
  24.                                         slider: false
  25.                                 });
  26.  
  27.                         },
  28.  
  29.                         data_replace: function(obj)
  30.                         {
  31.                                 if(!obj)
  32.                                         obj = $(this);
  33.  
  34.                                 obj.each(function()
  35.                                 {
  36.                                         val = $(this).val();
  37.  
  38.                                         if(!val)
  39.                                         {
  40.                                                 $(this).val($(this).attr('data-default'));
  41.                                         }
  42.  
  43.                                         $(this).focus(function()
  44.                                         {
  45.                                                 if($(this).val() == $(this).attr('data-default'))
  46.                                                 {
  47.                                                         $(this).val('');
  48.                                                 }
  49.  
  50.                                         }).blur(function()
  51.                                         {
  52.                                                 if($(this).val() == '')
  53.                                                 {
  54.                                                         $(this).val($(this).attr('data-default'));
  55.                                                 }
  56.                                         });
  57.                                 });
  58.                         },
  59.                         form_replace: function(obj)
  60.                         {
  61.                                 if(!obj)
  62.                                         obj = $(this);
  63.  
  64.                                 obj.each(function()
  65.                                 {
  66.                                         campos = $(this).find('[data-default]');
  67.                                         count = campos.length;
  68.  
  69.                                         if(count > 0)
  70.                                         {
  71.  
  72.                                                 $(this).submit(function(e)
  73.                                                 {
  74.                                                         e.preventDefault();
  75.  
  76.                                                         campos.each(function()
  77.                                                         {
  78.                                                                 if($(this).val() == $(this).attr('data-default'))
  79.                                                                 {
  80.                                                                         $(this).val('');
  81.                                                                 }
  82.                                                         });
  83.  
  84.                                                         if($(this).find('[class*=validate]'))
  85.                                                         {
  86.                                                                 if(!$(this).validationEngine('validate'))
  87.                                                                         return false;
  88.                                                         }
  89.  
  90.                                                         this.submit();
  91.                                                 });
  92.  
  93.                                         }
  94.                                 });
  95.                         }
  96.                 },
  97.                 click: function(obj, fn)
  98.                 {
  99.                         if(!obj)
  100.                                 obj = $(this);
  101.                         inject.unbind.click(obj);
  102.                         obj.on('click', fn);
  103.                 },
  104.                 unbind :
  105.                 {
  106.                         click   :
  107.                                         function(obj)
  108.                                         {
  109.                                                 if(!obj)
  110.                                                         obj = $(this);
  111.                                                 obj.off('click');
  112.                                         },
  113.                         blur    :
  114.                                         function(obj)
  115.                                         {
  116.                                                 if(!obj)
  117.                                                         obj = $(this);
  118.                                                 obj.off('blur');
  119.                                         },
  120.                         focus   :
  121.                                         function(obj)
  122.                                         {
  123.                                                 if(!obj)
  124.                                                         obj = $(this);
  125.                                                 obj.off('focus');
  126.                                         },
  127.                         submit  :
  128.                                         function(obj)
  129.                                         {
  130.                                                 if(!obj)
  131.                                                         obj = $(this);
  132.                                                 obj.off('submit');
  133.                                         }
  134.                 }
  135.         }
  136.  
  137. })(jQuery);
  138.  
  139. //
  140.  
  141. inject.input.data_replace($('[data-default]')); inject.input.form_replace($('form:has("[data-default]")'));