Created
June 7, 2012 22:42
-
-
Save jonraasch/2892148 to your computer and use it in GitHub Desktop.
HTML5 placeholder fallback that handles passwords (jquery)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* JS fallback for HTML5 placeholder | |
* requires jQuery and Modernizr (to detect support) | |
* by Jon Raasch - @jonraasch - http://jonraasch.com/ | |
* modified script from Nico Hagenburger: http://bit.ly/LgrkT0 | |
*/ | |
if(!Modernizr.input.placeholder){ | |
$('[placeholder]').focus(function() { | |
var input = $(this); | |
if (input.val() == input.attr('placeholder')) { | |
input.val(''); | |
input.removeClass('placeholder'); | |
} | |
}).blur(function() { | |
var input = $(this); | |
if (input.val() == '' || input.val() == input.attr('placeholder')) { | |
// if password input have to clone it as a text input and then remove this later (otherwise it will show up as ******) | |
if ( input.attr('type') == 'password' ) { | |
var newInput = input.clone(); | |
newInput.attr('type', 'text'); | |
newInput.val(input.attr('placeholder')); | |
newInput.addClass('placeholder clone'); | |
newInput.insertAfter(input); | |
input.hide(); | |
// add focus state to remove this input and show / focus the original | |
newInput.focus(function() { | |
$(this).remove(); | |
input.show().focus(); | |
}); | |
} | |
else { | |
input.addClass('placeholder'); | |
input.val(input.attr('placeholder')); | |
} | |
} | |
}).blur(); | |
$('[placeholder]').parents('form').submit(function() { | |
$(this).find('[placeholder]').each(function() { | |
var input = $(this); | |
if ( input.hasClass('clone') ) { | |
input.remove(); | |
return; | |
} | |
if (input.val() == input.attr('placeholder')) { | |
input.val(''); | |
} | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome - thanks! Worked perfectly.