-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3 - Toggling multiple password fields.html
79 lines (66 loc) · 2.56 KB
/
Day3 - Toggling multiple password fields.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<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>
/* Example of forEach function
var sandwiches = ['turkey', 'tuna', 'ham', 'pb&j'];
// logs 0, tuna, 1, ham, 2, turkey, 3, pb&j
sandwiches.forEach(function (sandwich, index) {
console.log(index) // index
console.log(sandwich) // value
}); */
// You can convert NodeLists into Arrays by passing them into Array.prototype.slice.call(), which will apply the Array.slice() method to other array-like objects.
//my approach to do the task
// const passwords = Array.prototype.slice.call(document.querySelectorAll('#current-password, #new-password'));
// const showPasswords = document.querySelector('#show-passwords');
// showPasswords.addEventListener("click", function () {
// if (showPasswords.checked) {
// passwords.forEach(function (password) {
// password.type = 'text';
// })
// } else {
// passwords.forEach(function (password) {
// password.type = 'password';
// })
// }
// })
//Chris's approach to do the task
const passwords = Array.prototype.slice.call(document.querySelectorAll('[type = "password"]'));
const showPasswords = document.querySelector('#show-passwords');
showPasswords.addEventListener("click",
function (event) {
passwords.forEach(function (password) {
if (showPasswords.checked) {
password.type = 'text';
} else {
password.type = 'password';
}
});
});
</script>
</body>
</html>