-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (69 loc) · 2.84 KB
/
script.js
File metadata and controls
90 lines (69 loc) · 2.84 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
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
function toggleNav() {
var nav = document.getElementById("nav");
if (nav.style.display === "block") {
nav.style.display = "none";
} else {
nav.style.display = "block";
}
}
document.getElementById('post-form').addEventListener('submit', function(event) {
event.preventDefault();
const postContent = document.getElementById('post-content').value;
const postImage = document.getElementById('post-image').files[0];
const postsSection = document.getElementById('posts');
const postElement = document.createElement('div');
postElement.classList.add('post');
const contentElement = document.createElement('p');
contentElement.textContent = postContent;
postElement.appendChild(contentElement);
if (postImage) {
const imageElement = document.createElement('img');
imageElement.src = URL.createObjectURL(postImage);
imageElement.alt = 'Post Image';
postElement.appendChild(imageElement);
}
postsSection.appendChild(postElement);
// Reset form
document.getElementById('post-form').reset();
});
document.addEventListener('DOMContentLoaded', function() {
loadPosts();
document.getElementById('post-form').addEventListener('submit', function(event) {
event.preventDefault();
const postContent = document.getElementById('post-content').value;
if (!postContent) return;
const postDate = new Date().toLocaleString();
const posts = getPostsFromLocalStorage();
posts.push({ content: postContent, date: postDate });
savePostsToLocalStorage(posts);
addPostToPage(postContent, postDate);
// Reset form
document.getElementById('post-form').reset();
});
});
function getPostsFromLocalStorage() {
const posts = localStorage.getItem('posts');
return posts ? JSON.parse(posts) : [];
}
function savePostsToLocalStorage(posts) {
localStorage.setItem('posts', JSON.stringify(posts));
}
function loadPosts() {
const posts = getPostsFromLocalStorage();
posts.forEach(post => {
addPostToPage(post.content, post.date);
});
}
function addPostToPage(content, date) {
const postsSection = document.getElementById('posts');
const postElement = document.createElement('div');
postElement.classList.add('post');
const dateElement = document.createElement('small');
dateElement.textContent = date;
dateElement.classList.add('post-date');
postElement.appendChild(dateElement);
const contentElement = document.createElement('p');
contentElement.textContent = content;
postElement.appendChild(contentElement);
postsSection.appendChild(postElement);
}