-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
48 lines (41 loc) · 1.65 KB
/
script2.js
File metadata and controls
48 lines (41 loc) · 1.65 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
function addComment() {
const commentInput = document.getElementById('comment-input');
const commentText = commentInput.value.trim();
if (commentText) {
let comments = JSON.parse(localStorage.getItem('comments')) || [];
comments.push(commentText);
localStorage.setItem('comments', JSON.stringify(comments));
displayComments();
commentInput.value = '';
} else {
alert('Будь ласка, введіть коментар.');
}
}
function displayComments() {
const commentsList = document.getElementById('comments-list');
commentsList.innerHTML = '';
let comments = JSON.parse(localStorage.getItem('comments')) || [];
comments.forEach(comment => {
const commentItem = document.createElement('div');
commentItem.classList.add('comment-item');
commentItem.textContent = comment;
commentsList.appendChild(commentItem);
});
}
window.onload = function() {
displayComments();
};
function deleteComment(button) {
const commentItem = button.parentElement;
const commentText = commentItem.querySelector("span").innerText;
let comments = JSON.parse(localStorage.getItem("comments")) || [];
comments = comments.filter((comment) => comment !== commentText);
localStorage.setItem("comments", JSON.stringify(comments));
commentItem.remove();
}
function deleteAllComments() {
if (confirm("Ви впевнені, що хочете видалити всі коментарі?")) {
localStorage.removeItem("comments");
document.getElementById("comments-list").innerHTML = "";
}
}