-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
70 lines (64 loc) · 2.29 KB
/
test.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
<!DOCTYPE html>
<html>
<head>
<title>Petition Website</title>
</head>
<body>
<h1>Sign the Petition</h1>
<p>We are petitioning for [insert cause here]. Please add your name to the list of supporters.</p>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<button type="submit">Sign</button>
</form>
<h2>Supporters:</h2>
<p>Number of supporters: <span id="counter">0</span></p>
<h2>Comments:</h2>
<form>
<label for="comment">Leave a comment:</label>
<textarea id="comment" name="comment"></textarea>
<br>
<label for="name">Name:</label>
<input type="text" id="comment-name" name="comment-name" required>
<br>
<button type="submit">Submit</button>
</form>
<ul id="comments-list"></ul>
<script>
// initialize counter to 0
let counter = 0;
// get DOM elements
const counterEl = document.getElementById('counter');
const supportersListEl = document.getElementById('supporters-list');
const commentsListEl = document.getElementById('comments-list');
const formEl = document.querySelector('form');
const commentFormEl = document.querySelectorAll('form')[1];
// add event listener to form submission
formEl.addEventListener('submit', (event) => {
event.preventDefault();
// increment counter and update DOM
counter++;
counterEl.textContent = counter;
// reset form
formEl.reset();
});
// add event listener to comment form submission
commentFormEl.addEventListener('submit', (event) => {
event.preventDefault();
// get form data
const comment = document.getElementById('comment').value;
const commentName = document.getElementById('comment-name').value;
// add comment to list
const liEl = document.createElement('li');
liEl.innerHTML = `<strong>${commentName}:</strong> ${comment}`;
commentsListEl.appendChild(liEl);
// reset form
commentFormEl.reset();
});
</script>
</body>
</html>