-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
64 lines (57 loc) · 2.15 KB
/
Copy pathscripts.js
File metadata and controls
64 lines (57 loc) · 2.15 KB
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
const form = document.getElementById('chat-form');
const textarea = form.querySelector('textarea[name="message"]');
const feedList = document.getElementById('feed-list');
const signupOverlay = document.getElementById('signup-overlay');
const signupForm = document.getElementById('signup-form');
const signupName = document.getElementById('signup-name');
let username = localStorage.getItem('username');
if (!username) {
signupOverlay.classList.remove('hidden');
}
signupForm.addEventListener('submit', (e) => {
e.preventDefault();
username = signupName.value.trim();
if (username) {
localStorage.setItem('username', username);
signupOverlay.classList.add('hidden');
}
});
function buildCommentElement(text, user) {
const li = document.createElement('li');
li.innerHTML = `<strong>${user}</strong><p>${text}</p><button class="reply-btn">Antworten</button>`;
const replies = document.createElement('ul');
li.appendChild(replies);
const replyForm = document.createElement('form');
replyForm.classList.add('reply-form', 'hidden');
replyForm.innerHTML = '<textarea></textarea><button type="submit">Senden</button>';
li.appendChild(replyForm);
return li;
}
form.addEventListener('submit', function (event) {
event.preventDefault();
const text = textarea.value.trim();
if (text === '' || !username) return;
const li = buildCommentElement(text, username);
feedList.prepend(li);
textarea.value = '';
});
feedList.addEventListener('click', (event) => {
if (event.target.classList.contains('reply-btn')) {
const li = event.target.closest('li');
const replyForm = li.querySelector('.reply-form');
replyForm.classList.toggle('hidden');
}
});
feedList.addEventListener('submit', (event) => {
if (event.target.classList.contains('reply-form')) {
event.preventDefault();
const textarea = event.target.querySelector('textarea');
const text = textarea.value.trim();
if (text === '' || !username) return;
const replies = event.target.parentElement.querySelector('ul');
const newLi = buildCommentElement(text, username);
replies.prepend(newLi);
textarea.value = '';
event.target.classList.add('hidden');
}
});