Skip to content

Instantly share code, notes, and snippets.

@mynameispj
Created May 15, 2012 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mynameispj/2704888 to your computer and use it in GitHub Desktop.
Save mynameispj/2704888 to your computer and use it in GitHub Desktop.
Swap text input for password input on focus / blur
$('input').focus(function() {
if ($(this).hasClass('passwordLabel')) {
$(this).hide();
$(this).next('input').show();
$(this).next('input').focus();
}
if (this.value == this.defaultValue){
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
});
$('input').blur(function() {
if ($.trim(this.value) == ''){
if ($(this).hasClass('passwordField')) {
var parentInputValue = $(this).prev('input').attr('default-value');
$(this).hide();
$(this).prev('input').show();
$(this).prev('input').attr('value',parentInputValue);
} else {
this.value = (this.defaultValue ? this.defaultValue : '');
}
}
});
<input type="text" value="Email address" />
<input type="text" class="passwordLabel" default-value="Password" value="Password" />
<input type="password" class="passwordField" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment