Advertisement
akeif

Change input field placeholder text with jQuery w/ password

Mar 3rd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script is an update of http://www.perfectline.co/2009/10/change-input-field-placeholder-with-jquery/
  2. // If you don't need input[type=password] support use http://pastebin.com/w7PhFQdu
  3.  
  4. $(document).ready( function() {
  5.     $(".placeholder").each( function( ) {
  6.    
  7.         var elem = $(this);
  8.         var text = elem.attr('rel');
  9.  
  10.         if( elem.attr('type') === 'password' )
  11.             elem.type = 'password';
  12.        
  13.         if ( elem.val() === '' ) {
  14.             elem.val( text );
  15.            
  16.             if( elem.type === 'password' )
  17.                 elem.attr('type', 'text' );
  18.         }
  19.    
  20.         elem.focus( function() {
  21.             if ( elem.val() === text ) {
  22.                 elem.val('');
  23.                
  24.                 if( elem.type === 'password' )
  25.                     elem.attr('type', 'password');
  26.             }
  27.         });
  28.  
  29.         elem.blur( function() {
  30.             if ( elem.val() === '' ) {
  31.                 elem.val( text );
  32.                
  33.                 if( elem.type === 'password' )
  34.                     elem.attr('type', 'text');
  35.             }
  36.         });
  37.        
  38.         var form = elem.parents('form:first');
  39.  
  40.         form.submit( function() {
  41.             if (elem.val() === text)
  42.                 elem.val("");
  43.         });
  44.        
  45.     });
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement