Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tyte/55e1e6af741ba8ad59dbd64fc8a52eb4 to your computer and use it in GitHub Desktop.
Save tyte/55e1e6af741ba8ad59dbd64fc8a52eb4 to your computer and use it in GitHub Desktop.
JS toggle multiple passwords visibility
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Password Visibility - Multiple Fields</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 - Multiple Fields</h1>
<p>Enter your current password and new password below.</p>
<form>
<div>
<label for="current-password">Current Password</label>
<input type="password" name="current-password" id="current-password">
</div>
<div>
<label for="new-password">New Password</label>
<input type="password" name="new-password" id="new-password">
</div>
<div>
<label for="show-passwords">
<input type="checkbox" name="show-passwords" id="show-passwords">
Show passwords
</label>
</div>
<p>
<button type="submit">Change Passwords</button>
</p>
</form>
<script>
let showPasswords = document.getElementById('show-passwords');
let passwordFields = document.querySelectorAll('[type="password"]');
showPasswords.addEventListener('click', function (event) {
if (showPasswords.checked) {
for (let passwordField of passwordFields) {
passwordField.type = "text";
}
} else {
for (let passwordField of passwordFields) {
passwordField.type = "password";
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment