-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla-2-alt.html
75 lines (64 loc) · 1.55 KB
/
vanilla-2-alt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
≤<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vanilla js project 2 alt</title>
</head>
<body>
<!-- When a user clicks on the #show-passwords checkbox, it should show the text for the #current-password and #new-password fields if it’s checked, and mask it if it’s unchecked. -->
<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>
// Variables
// Get the password toggle
const toggle = document.querySelector('#show-passwords');
// Get the password fields
const passwords = Array.from(
document.querySelectorAll('[type="password"]')
);
//
// Functions
//
/**
* Toggle the visibility of a password field
* based on a checkbox
*
* @param {Object} checkbox The checkbox
* @param {Object} field The password field
*/
function togglePassword (checkbox, field) {
field.type = checkbox.checked ? 'text' : 'password';
}
/**
* Handle change events
*/
function handleChange () {
passwords.forEach(password => {
togglePassword(this, password);
});
}
//
// Inits & Event Listeners
//
// Handle change events
toggle.addEventListener('change', handleChange);
</script>
</body>
</html>