-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
55 lines (41 loc) · 1.23 KB
/
post.js
File metadata and controls
55 lines (41 loc) · 1.23 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
// Select elements
const commentForm = document.getElementById("commentForm");
const commentList = document.getElementById("commentList");
// Load existing comments from localStorage
let comments = JSON.parse(localStorage.getItem("comments")) || [];
// Function to display comments
function displayComments() {
commentList.innerHTML = "";
comments.forEach((c, index) => {
const div = document.createElement("div");
div.classList.add("comment-item");
div.innerHTML = `
<p><strong>${c.name}</strong></p>
<p>${c.text}</p>
<small>${c.date}</small>
<hr>
`;
commentList.appendChild(div);
});
}
// Handle form submission
commentForm.addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("name").value.trim();
const commentText = document.getElementById("comment").value.trim();
if (!name || !commentText) return;
const newComment = {
name: name,
text: commentText,
date: new Date().toLocaleString()
};
comments.push(newComment);
// Save to localStorage
localStorage.setItem("comments", JSON.stringify(comments));
// Refresh UI
displayComments();
// Clear form
commentForm.reset();
});
// Initial load
displayComments();