A blog app is the project where front-end skills become a real product. You will build creating, reading, and deleting posts — the famous CRUD pattern — and store everything in the browser so it survives a refresh. If you want to see what a simple working blog looks like in practice, I write short practical guides on my Skill Web blog — this app is the same idea, in miniature.
The plan
- A form to write a post (title and body).
- A list of posts, newest first, rendered from an array.
- Every post saved to
localStorage, so a refresh does not wipe it. - A delete button on each post.
The HTML
<form id="postForm">
<input id="title" type="text" placeholder="Post title" required>
<textarea id="body" placeholder="Write your post..." required></textarea>
<button type="submit">Publish</button>
</form>
<div id="posts"></div>
The JavaScript
const form = document.getElementById('postForm');
const postsBox = document.getElementById('posts');
// Load saved posts, or start empty
let posts = JSON.parse(localStorage.getItem('posts') || '[]');
function save() {
localStorage.setItem('posts', JSON.stringify(posts));
}
function render() {
postsBox.innerHTML = posts.map((p, i) => `
<article>
<h2>${escapeHtml(p.title)}</h2>
<p>${escapeHtml(p.body)}</p>
<button data-delete="${i}">Delete</button>
</article>
`).join('');
}
// Escape user input so a <script> tag in a post cannot run
function escapeHtml(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
}
form.addEventListener('submit', (e) => {
e.preventDefault();
const title = document.getElementById('title').value.trim();
const body = document.getElementById('body').value.trim();
if (!title || !body) return;
posts.unshift({ title, body, date: new Date().toISOString() });
save();
render();
form.reset();
});
postsBox.addEventListener('click', (e) => {
const index = e.target.dataset.delete;
if (index === undefined) return;
posts.splice(Number(index), 1);
save();
render();
});
render();
Three details beginners miss
-
localStorageonly stores strings.JSON.stringifygoing in andJSON.parsecoming out is not optional — it is the whole mechanism. -
Escape user input. This is the one security lesson every beginner must learn early: never put raw user text into
innerHTML. A post titled<img src=x onerror=alert(1)>would run code on every reader's browser without escaping. -
Newest first with
unshift. Adding to the front of the array means the render order is already right, with no sorting needed.
What to add next
An edit button, tags on each post, a search box, and a reading-time estimate ("3 min read") computed from the word count. Then, when you are ready, swap localStorage for a real backend and your blog works for everyone, not just one browser.
Top comments (0)