Advertisement
amereservant

Modernizr - Add HTML5 placeholder support

Aug 17th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.     * Add Placeholder Support
  3.     *
  4.     * This adds backwards compatibility for HTML5 placeholder attributes
  5.     * @since    0.0.1
  6.     */
  7.     function addPlHolderSupport() {
  8.         var plhName = 'placeholder';
  9.         var inpt;
  10.         if(Modernizr && !Modernizr.input.placeholder)
  11.         {
  12.             $('['+plhName+']').bind({
  13.                 focus   : function(){
  14.                     inpt = $(this);
  15.                     if(inpt.val() == inpt.attr(plhName)) {
  16.                         inpt.val('');
  17.                         inpt.removeClass(plhName);
  18.                     }
  19.                 },
  20.                 blur    : function(){
  21.                     inpt = $(this);
  22.                     if(inpt.val() == '' || inpt.val() == inpt.attr(plhName))
  23.                     {
  24.                         inpt.addClass(plhName);
  25.                         inpt.val(inpt.attr(plhName));
  26.                     }
  27.                 }
  28.             }).blur();
  29.             $('['+plhName+']').parents('form').bind('submit', function(){          
  30.                 $(this).find('['+plhName+']').each(function(){
  31.                     inpt = $(this);
  32.                     if(inpt.val() == inpt.attr(plhName)){
  33.                         inpt.val('');
  34.                     }
  35.                 });
  36.             });
  37.         }
  38.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement