Created
May 15, 2012 20:30
-
-
Save mynameispj/2704888 to your computer and use it in GitHub Desktop.
Swap text input for password input on focus / blur
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
$('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 : ''); | |
} | |
} | |
}); |
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
<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