-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj3-toggle-mutliple-forms.html
118 lines (92 loc) · 2.63 KB
/
proj3-toggle-mutliple-forms.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Password Visibility - Multiple Forms</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 Forms</h1>
<h2>Change Username</h2>
<p>Enter your username and password to change your username.</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-password" id="show-password" data-pwd-toggle="#password">
Show password
</label>
</div>
<p>
<button type="submit">Change Username</button>
</p>
</form>
<h2>Change Password</h2>
<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" data-pwd-toggle="#current-password, #new-password">
Show passwords
</label>
</div>
<p>
<button type="submit">Change Passwords</button>
</p>
</form>
<script>
// init variables
let changeUsernameFields = document.querySelector('[name="password"]')
let changePasswordFields = document.querySelectorAll('[data-bottompass]');
// capture events for both checkboxes
document.addEventListener('click', function(event) {
// if click not in the checkboxes, terminate the callback function
if (!event.target.matches('[data-pwd-toggle]')) return;
// else, get the password fields using the selectors
let pwdSelector = event.target.getAttribute('data-pwd-toggle');
let passwordFields = document.querySelectorAll(pwdSelector);
// iterate on the password fields to toggle between password <-> text
for (let password of passwordFields) {
if (event.target.checked) {
password.type = 'text';
} else {
password.type = 'password';
}
}
});
</script>
</body>
</html>