-
Notifications
You must be signed in to change notification settings - Fork 0
/
question9.htm
136 lines (129 loc) · 3.6 KB
/
question9.htm
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>JS For Beginners</title>
<!-- <link rel="stylesheet" href="style.css"> -->
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
line-height: 1.6;
}
ul {
list-style: none;
}
ul li {
padding: 5px;
background: #f4f4f4;
margin: 5px 0;
}
header {
background: #f4f4f4;
padding: 1rem;
text-align: center;
}
.container {
margin: auto;
width: 500px;
overflow: auto;
padding: 3rem 2rem;
}
#my-form {
padding: 2rem;
background: #f4f4f4;
}
#my-form label {
display: block;
}
#my-form input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border-radius: 5px;
border: 1px solid #ccc;
}
.btn {
display: block;
width: 100%;
padding: 10px 10px;
border: 0;
background: #333;
color: #fff;
border-radius: 5px;
margin: 5px 0;
}
</style>
</head>
<body>
<header>
<h1>JS For Beginners</h1>
</header>
<section class="container">
<form id="my-form" novalidate>
<h1>Add User</h1>
<div class="msg"></div>
<div>
<label for="name">Name:</label>
<input type="text" id="name" required>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" required>
</div>
<input class="btn" type="submit" value="Submit">
</form>
<ul id="users"></ul>
</section>
<script>
let form = document.querySelector('#my-form');
let body = document.querySelector('body');
let btn = document.querySelector('.btn')
let nameinput = document.getElementById('name');
let mailinput = document.getElementById('email');
let msg = document.querySelector('.msg');
let ul = document.querySelector('ul');
btn.addEventListener('mouseover', onclick);
function onclick(e) {
e.preventDefault();
body.style.background = "#333";
body.style.color = '#bbb';
}
btn.addEventListener('mouseout', (e) => {
body.style.background = '#fff';
body.style.color = '#333';
})
btn.addEventListener('click',(e) => {
e.preventDefault()
if(!nameinput.value || !mailinput.value){
msg.style.background='orangered';
msg.style.color='white';
msg.style.margin='3px';
msg.style.padding='5px';
msg.innerHTML = "Please enter your details."
setTimeout(() => msg.remove(), 2000);
}
else{
let li = document.createElement('li');
li.appendChild(document.createTextNode(nameinput.value));
ul.appendChild(li);
let li_1 = document.createElement('li');
li_1.appendChild(document.createTextNode(`${mailinput.value}`));
ul.appendChild(li_1);
// console.log(nameinput.value);
// console.log(mailinput.value);
localStorage.setItem('userName',nameinput.value);
localStorage.setItem('userEmail',mailinput.value)
nameinput.value = '';
mailinput.value = '';
}
});
</script>
</body>
</html>