jQuery Mini Project: Task Board
In this project you build a small task board step by step, using nearly everything from the course: selectors, events, effects, DOM manipulation, traversing, filtering, delegation and AJAX-style saving. Each step is a complete, runnable page that extends the previous one, so you can stop at any step and experiment.
What the finished widget does:
- Add tasks with a title and a priority.
- Mark tasks as done, delete them, and edit a title by double-clicking it.
- Filter by All / Active / Done and show a live counter.
- "Save" the board to a (fake) server and load it again.
Step 1: Markup and adding tasks
We start with static HTML and a submit handler that creates a list item. Each task is built with $("<li>") and .text(), so user input is always escaped.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 14px; max-width: 420px; }
#tasks { list-style: none; padding: 0; }
#tasks li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; margin: 4px 0; background: #f4f6f8; border-radius: 6px; border-left: 4px solid #999; }
#tasks li[data-priority="high"] { border-left-color: #c0392b; }
#tasks li[data-priority="low"] { border-left-color: #27ae60; }
</style>
</head>
<body>
<form id="new-task">
<input id="title" placeholder="What needs doing?" autocomplete="off">
<select id="priority"><option value="low">low</option><option value="normal" selected>normal</option><option value="high">high</option></select>
<button>Add</button>
</form>
<ul id="tasks"></ul>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function createTask(title, priority) {
return $("<li>")
.attr("data-priority", priority)
.append($("<span class='title'>").text(title));
}
$("#new-task").on("submit", function (e) {
e.preventDefault();
const title = $("#title").val().trim();
if (!title) return;
createTask(title, $("#priority").val()).appendTo("#tasks");
$("#title").val("").trigger("focus");
console.log("tasks:", $("#tasks li").length);
});
</script>
</body>
</html>Step 2: Done and delete with delegation
Tasks are created dynamically, so the list gets one delegated handler per action. A checkbox toggles a done class; the ✕ button fades the item out and then removes it.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 14px; max-width: 420px; }
#tasks { list-style: none; padding: 0; }
#tasks li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; margin: 4px 0; background: #f4f6f8; border-radius: 6px; border-left: 4px solid #999; }
#tasks li[data-priority="high"] { border-left-color: #c0392b; }
#tasks li[data-priority="low"] { border-left-color: #27ae60; }
#tasks li.done .title { text-decoration: line-through; color: #999; }
#tasks .title { flex: 1; }
#tasks .del { border: 0; background: none; color: #c0392b; cursor: pointer; }
</style>
</head>
<body>
<form id="new-task">
<input id="title" placeholder="What needs doing?" autocomplete="off">
<select id="priority"><option value="low">low</option><option value="normal" selected>normal</option><option value="high">high</option></select>
<button>Add</button>
</form>
<ul id="tasks"></ul>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function createTask(title, priority, done) {
return $("<li>")
.attr("data-priority", priority)
.toggleClass("done", !!done)
.append(
$("<input type='checkbox' class='toggle'>").prop("checked", !!done),
$("<span class='title'>").text(title),
$("<button type='button' class='del' title='Delete'>").text("✕")
);
}
// seed data
$("#tasks").append(
createTask("Read the jQuery docs", "normal"),
createTask("Fix the login bug", "high"),
createTask("Water the plants", "low", true)
);
$("#new-task").on("submit", function (e) {
e.preventDefault();
const title = $("#title").val().trim();
if (!title) return;
createTask(title, $("#priority").val()).hide().appendTo("#tasks").slideDown(150);
$("#title").val("").trigger("focus");
});
$("#tasks")
.on("change", ".toggle", function () {
$(this).closest("li").toggleClass("done", this.checked);
console.log("done:", $("#tasks li.done").length, "of", $("#tasks li").length);
})
.on("click", ".del", function () {
$(this).closest("li").fadeOut(200, function () { $(this).remove(); });
});
</script>
</body>
</html>Step 3: Filters and a live counter
Filter buttons set a data-filter value; one applyFilter() function shows or hides tasks with .filter() and .not(). A refresh() function updates the counter after every change — call it from each handler.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 14px; max-width: 420px; }
#tasks { list-style: none; padding: 0; }
#tasks li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; margin: 4px 0; background: #f4f6f8; border-radius: 6px; border-left: 4px solid #999; }
#tasks li[data-priority="high"] { border-left-color: #c0392b; }
#tasks li[data-priority="low"] { border-left-color: #27ae60; }
#tasks li.done .title { text-decoration: line-through; color: #999; }
#tasks .title { flex: 1; }
#tasks .del { border: 0; background: none; color: #c0392b; cursor: pointer; }
.bar { display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
.filters button { border: 1px solid #0769ad; background: #fff; color: #0769ad; padding: 2px 8px; cursor: pointer; }
.filters button.on { background: #0769ad; color: #fff; }
</style>
</head>
<body>
<form id="new-task">
<input id="title" placeholder="What needs doing?" autocomplete="off">
<select id="priority"><option value="low">low</option><option value="normal" selected>normal</option><option value="high">high</option></select>
<button>Add</button>
</form>
<ul id="tasks"></ul>
<div class="bar">
<span id="count"></span>
<span class="filters">
<button data-filter="all" class="on">All</button><button data-filter="active">Active</button><button data-filter="done">Done</button>
</span>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
let filter = "all";
function createTask(title, priority, done) {
return $("<li>").attr("data-priority", priority).toggleClass("done", !!done).append(
$("<input type='checkbox' class='toggle'>").prop("checked", !!done),
$("<span class='title'>").text(title),
$("<button type='button' class='del' title='Delete'>").text("✕"));
}
function applyFilter() {
const $all = $("#tasks li");
const $visible = filter === "all" ? $all
: filter === "done" ? $all.filter(".done")
: $all.not(".done");
$all.not($visible).hide();
$visible.show();
}
function refresh() {
const left = $("#tasks li").not(".done").length;
$("#count").text(left + (left === 1 ? " task" : " tasks") + " left");
applyFilter();
}
$("#tasks").append(
createTask("Read the jQuery docs", "normal"),
createTask("Fix the login bug", "high"),
createTask("Water the plants", "low", true));
refresh();
$("#new-task").on("submit", function (e) {
e.preventDefault();
const title = $("#title").val().trim();
if (!title) return;
createTask(title, $("#priority").val()).appendTo("#tasks");
$("#title").val("").trigger("focus");
refresh();
});
$("#tasks")
.on("change", ".toggle", function () {
$(this).closest("li").toggleClass("done", this.checked);
refresh();
})
.on("click", ".del", function () {
$(this).closest("li").fadeOut(200, function () { $(this).remove(); refresh(); });
});
$(".filters").on("click", "button", function () {
filter = $(this).data("filter");
$(this).addClass("on").siblings().removeClass("on");
refresh();
console.log("filter:", filter, "| visible:", $("#tasks li:visible").length);
});
</script>
</body>
</html>Step 4: Edit in place, save and load
The final version adds:
- Double-click to edit: the title
<span>is swapped for an<input>; Enter or leaving the field saves, Escape cancels. - Save / Load through
$.ajaxto/api/board— answered by a fake server that keeps the JSON in memory, just like a real backend would. - Tasks are converted to plain data with
.map()and rebuilt from data with$.map().
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; font-size: 14px; max-width: 440px; }
#tasks { list-style: none; padding: 0; min-height: 20px; }
#tasks li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; margin: 4px 0; background: #f4f6f8; border-radius: 6px; border-left: 4px solid #999; }
#tasks li[data-priority="high"] { border-left-color: #c0392b; }
#tasks li[data-priority="low"] { border-left-color: #27ae60; }
#tasks li.done .title { text-decoration: line-through; color: #999; }
#tasks .title { flex: 1; cursor: text; }
#tasks .edit { flex: 1; }
#tasks .del { border: 0; background: none; color: #c0392b; cursor: pointer; }
.bar { display: flex; justify-content: space-between; align-items: center; font-size: 13px; gap: 6px; }
.filters button { border: 1px solid #0769ad; background: #fff; color: #0769ad; padding: 2px 8px; cursor: pointer; }
.filters button.on { background: #0769ad; color: #fff; }
#status { color: #2e7d32; font-size: 12px; }
</style>
</head>
<body>
<form id="new-task">
<input id="title" placeholder="What needs doing?" autocomplete="off">
<select id="priority"><option value="low">low</option><option value="normal" selected>normal</option><option value="high">high</option></select>
<button>Add</button>
</form>
<ul id="tasks"></ul>
<div class="bar">
<span id="count"></span>
<span class="filters">
<button data-filter="all" class="on">All</button><button data-filter="active">Active</button><button data-filter="done">Done</button>
</span>
</div>
<p><button id="save">Save</button> <button id="load">Load</button> <span id="status"></span></p>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
// ---- fake server: GET/PUT /api/board ----
let stored = JSON.stringify([
{ title: "Read the jQuery docs", priority: "normal", done: false },
{ title: "Fix the login bug", priority: "high", done: false },
{ title: "Water the plants", priority: "low", done: true }
]);
$.ajaxTransport("+*", function (o) {
if (o.url !== "/api/board") return;
let t;
return {
send(headers, complete) {
t = setTimeout(() => {
if (o.type === "PUT") { stored = o.data; complete(200, "OK", { text: '{"saved":true}' }); }
else complete(200, "OK", { text: stored });
}, 300);
},
abort() { clearTimeout(t); }
};
});
// ------------------------------------------
let filter = "all";
function createTask(task) {
return $("<li>").attr("data-priority", task.priority).toggleClass("done", !!task.done).append(
$("<input type='checkbox' class='toggle'>").prop("checked", !!task.done),
$("<span class='title'>").text(task.title),
$("<button type='button' class='del' title='Delete'>").text("✕"));
}
function toData() {
return $("#tasks li").map(function () {
const $li = $(this);
return { title: $li.find(".title").text(), priority: $li.attr("data-priority"), done: $li.hasClass("done") };
}).get();
}
function render(tasks) {
$("#tasks").empty().append($.map(tasks, createTask));
refresh();
}
function refresh() {
const $all = $("#tasks li");
const left = $all.not(".done").length;
$("#count").text(left + (left === 1 ? " task" : " tasks") + " left");
const $visible = filter === "all" ? $all : filter === "done" ? $all.filter(".done") : $all.not(".done");
$all.not($visible).hide();
$visible.show();
}
function status(text) {
$("#status").stop(true, true).text(text).show().delay(1200).fadeOut(400);
}
// add
$("#new-task").on("submit", function (e) {
e.preventDefault();
const title = $("#title").val().trim();
if (!title) return;
createTask({ title, priority: $("#priority").val(), done: false }).appendTo("#tasks");
$("#title").val("").trigger("focus");
refresh();
});
// toggle, delete, edit — all delegated
$("#tasks")
.on("change", ".toggle", function () {
$(this).closest("li").toggleClass("done", this.checked);
refresh();
})
.on("click", ".del", function () {
$(this).closest("li").fadeOut(200, function () { $(this).remove(); refresh(); });
})
.on("dblclick", ".title", function () {
const $title = $(this);
const $input = $("<input class='edit'>").val($title.text());
$title.hide().after($input);
$input.trigger("focus");
})
.on("keydown", ".edit", function (e) {
if (e.key === "Enter") $(this).trigger("blur");
if (e.key === "Escape") $(this).val($(this).prev(".title").text()).trigger("blur");
})
.on("blur", ".edit", function () {
const $input = $(this);
const text = $input.val().trim();
const $title = $input.prev(".title");
if (text) $title.text(text);
$title.show();
$input.remove();
console.log("edited:", $title.text());
});
// filters
$(".filters").on("click", "button", function () {
filter = $(this).data("filter");
$(this).addClass("on").siblings().removeClass("on");
refresh();
});
// save / load
$("#save").on("click", function () {
const data = toData();
$.ajax({ url: "/api/board", method: "PUT", contentType: "application/json", data: JSON.stringify(data), dataType: "json" })
.done(() => { status("Saved " + data.length + " tasks ✔"); console.log("saved:", data); });
});
$("#load").on("click", function () {
$("#status").stop(true, true).show().text("Loading…");
$.getJSON("/api/board").done(function (tasks) {
render(tasks);
status("Loaded " + tasks.length + " tasks");
});
});
$("#load").trigger("click"); // initial load
</script>
</body>
</html>Try this sequence: add two tasks, mark one done, rename another by double-clicking it, press Save, delete everything, then press Load — the saved board comes back.
Ideas to extend it
| Idea | Hint |
|---|---|
| Sort by priority | Sort the array from toData() and call render() |
| "Clear completed" button | $("#tasks li.done").remove(); refresh(); |
| Persist in the browser | localStorage.setItem("board", JSON.stringify(toData())) |
| Drag to reorder | jQuery UI's .sortable(), or the native HTML Drag and Drop API |
| Rewrite in vanilla JS | Use the tables from jQuery vs Modern JavaScript as a checklist |
Test yourself
Why are the toggle, delete and edit handlers attached to #tasks with a selector?
- To make them run twice
- Because tasks are created and re-rendered dynamically, so delegation covers all of them
- Because checkboxes cannot have handlers
- To avoid using
$(this)
What does $("#tasks li").map(function () { return {…}; }).get() produce?
- A jQuery object of objects
- A plain array of task objects
- A JSON string
- The number of tasks
Why are task titles inserted with .text() rather than inside an HTML string?
.text()is faster to type- It escapes user input, preventing HTML/script injection
- HTML strings cannot be appended
- Because of the fake server