Last active
April 4, 2022 15:54
-
-
Save tyte/d36a15204c6a781ab45ae2dd4439393e to your computer and use it in GitHub Desktop.
JS - toggle password visibility
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Password Visibility</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<style type="text/css"> | |
body { | |
margin: 0 auto; | |
max-width: 40em; | |
width: 88%; | |
} | |
label { | |
display: block; | |
width: 100%; | |
} | |
input { | |
margin-bottom: 1em; | |
} | |
[type="checkbox"] { | |
margin-bottom: 0; | |
margin-right: 0.25em; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Password Visibility</h1> | |
<p>Enter your username and password to login.</p> | |
<form> | |
<div> | |
<label for="username">Username</label> | |
<input type="text" name="username" id="username"> | |
</div> | |
<div> | |
<label for="password">Password</label> | |
<input type="password" name="password" id="password"> | |
</div> | |
<div> | |
<label for="show-password"> | |
<input type="checkbox" name="show-passwords" id="show-password"> | |
Show password | |
</label> | |
</div> | |
<p> | |
<button type="submit">Log In</button> | |
</p> | |
</form> | |
<script> | |
// Your code goes here... | |
let showPassword = document.getElementById('show-password'); | |
let password = document.getElementById('password'); | |
showPassword.addEventListener('click', function (event) { | |
let inputType = password.getAttribute('type'); | |
if (inputType == 'password') { | |
password.setAttribute('type', 'text'); | |
} else { | |
password.setAttribute('type', 'password'); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment