June 18, 2020
How to Toggle Password Visibility Using Javascript
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <div> <input type="password" placeholder="Enter Password"> <label><input type="checkbox">Show</label> </div> <script src="script.js"></script> </body> </html> |
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 |
body { text-align: center; } div { display: inline-block; } input { display: inline-block; padding: 10px 20px; } |
Javascript
1 2 3 4 5 6 7 |
let input = document.querySelector('input[type="password"]'); let checkbox = document.querySelector('input[type="checkbox"]'); checkbox.addEventListener('click', () => { if(checkbox.checked) input.type = 'text'; else input.type = 'password'; }); |