-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1 - Toggle Password Visibility.html
87 lines (71 loc) · 2.25 KB
/
Day1 - Toggle Password Visibility.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
<!DOCTYPE html>
<html>
<head>
<title>Password Visibility</title>
<style type="text/css">
body {
margin: 1em 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>
// my approach to do the task
// const showBtn = document.querySelector("input#show-password");
// const password = document.querySelector("input#password");
// showBtn.addEventListener("click", function () {
// const attr = password.getAttribute("type");
// if (attr == "password") {
// password.setAttribute("type", "text");
// } else if (attr == "text") {
// password.setAttribute("type", "password");
// }
// })
//Chris's approach to do the task
const showBtn = document.querySelector("input#show-password");
const password = document.querySelector("input#password");
showBtn.addEventListener("click", function () {
if (showBtn.checked) {
password.type = "text";
} else {
password.type = "password";
}
}, false)
// false parametr allows you to choose either you want to use event bubbling or capturing -> https://www.w3schools.com/js/js_htmldom_eventlistener.asp
</script>
</body>
</html>