Password show/hide button

Show/hide password with the click of a button

by oktaviardi pratama

HTML

<input type="password" class="input-password" />
<a href="#show-password" class="toggle-password">Show password</a>

JavaScript

$('.toggle-password').click(function(e) {
    e.preventDefault();
    
    if($(this).prev('.input-password').attr('type') == 'text') {
        $(this).prev('.input-password').attr('type', 'password');
        console.log('now password');
        $(this).text('Show password');
    } else {
        $(this).prev('.input-password').attr('type', 'text');
        console.log('now text');
        $(this).text('Hide password');
    }
});