Skip to content

Instantly share code, notes, and snippets.

@philcon93
Last active October 27, 2017 04:28
Show Gist options
  • Save philcon93/2fff671833788e6f1e146ac333cfd120 to your computer and use it in GitHub Desktop.
Save philcon93/2fff671833788e6f1e146ac333cfd120 to your computer and use it in GitHub Desktop.
Show/hide password input field

Show/hide password input field

JS

This is allows you to see/hide the password input fields, making it a lot easier to input a create value.

$(document).ready(function(){
if($('input[type=password]').length > 0){
$('input[type=password]').each(function(){
var a = document.createElement('a');
var t = document.createTextNode("Show");
a.appendChild(t);
a.setAttribute('data-show', 'false');
a.setAttribute('title', 'Toggle display for password input');
a.classList.add('toggle-pwd');
a.href = "#";
$(this).wrap("<div class=\"wrapper-pwd\"></div>");
$(this).after(a);
});
}
});
$('body').on('click', '.toggle-pwd', function(e){
e.preventDefault();
var displayPwd = $(this).attr('data-show');
if(displayPwd == 'false'){
$(this).attr('data-show', 'true').text('Hide');
$(this).siblings('input').attr('type', 'text');
}else{
$(this).attr('data-show', 'false').text('Show');
$(this).siblings('input').attr('type', 'password');
}
});
.wrapper-pwd{
position: relative;
.toggle-pwd{
position: absolute;
top: 15px;
right: 10px;
letter-spacing: 2px;
font-size: 14px;
text-transform: uppercase;
text-decoration: none;
color: #808080;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment