简体   繁体   中英

JS Function to hide and show password doesn't work

Whi my function to hide and show my password does not work? Anyone could help me?

#HTML:

<div class="senha">
<i class="bi bi-key"></i>
<input type="password" required autofocus placeholder="senha" id="idsenha">
<i class="bi bi-eye"></i>
</div> 

JS:

let btn = document.querySelector('.bi-eye');

btn.addEventListener('click', function() {

    let input = document.querySelector('#idsenha');

    if(input.getAttribute('type') == 'password') {
        input.setAttribute('type', 'text');
    } else {
        input.setAttribute('type', 'password');
    }
});

Your code works.

 let btn = document.querySelector('.bi-eye'); btn.addEventListener('click', function() { let input = document.querySelector('#idsenha'); if (input.getAttribute('type') == 'password') { input.setAttribute('type', 'text'); } else { input.setAttribute('type', 'password'); } });
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.2.2/font/bootstrap-icons.css" /> <div class="senha"> <i class="bi bi-key"></i> <input type="password" required autofocus placeholder="senha" id="idsenha"> <i class="bi bi-eye"></i> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM