flowchart LR
Input[Type Task & Click Add] --> Add[Append to List]
Add --> Render[Display Item + Delete Button]
Render --> ClickDel{Click Delete?}
ClickDel -- Yes --> Remove[Remove Item from Array/DOM]
ClickDel -- No --> Render
Week 1 - Todo Lists and CSS
Today we’re going to focus on how to implement a todo list in svelte and utilise a CSS library.
We will work on some basic styling options with the W3.CSS library https://www.w3schools.com/w3css/.
You can view the whole reference table for the W3 css https://www.w3schools.com/w3css/w3css_references.asp.
To use this library, you can add the following line to your HTML file in the <head> section:
<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">Code Examples
This is a todo list in svelte. You can view this in the svelte playground here - Todo List in Svelte
<script>
import Todo from './Todo.svelte';
let text = $state("");
let todos = $state(["shopping", "read a book", "make some money"]);
function addTodo(){
todos.push(text);
text = "";
}
function removeTodo(i){
todos.splice(i,1);
}
</script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">
<div class="w3-container w3-padding">
<div class="w3-container w3-padding">
<input bind:value={text}>
<button onclick={addTodo}>Add</button>
</div>
<div class="w3-container w3-blue">
{#each todos as t, i}
<div class="w3-container w3-padding">
<div class="w3-card w3-padding w3-green">Todo: {(i+1)} - {t} <button class="w3-right" onclick={() => removeTodo(i)}>x </button></div>
</div>
{/each}
</div>
</div>This is a todo list in vanilla JS. You can view this in codepen here - Todo List in Vanilla JS
<html>
<head>
<title>Todo List</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/6/w3.css">
</head>
<body>
<div class="w3-container w3-padding">
<div class="w3-container w3-padding">
<input id="inputtext" placeholder="Enter a task">
<button onclick="addTodo()">Add</button>
</div>
<div class="w3-container w3-blue" id="todolist"></div>
</div>
<script>
const todolist = document.getElementById("todolist");
const inputtext = document.getElementById("inputtext");
let todos = ["shopping", "read a book", "make some money"];
function addTodo() {
const text = inputtext.value.trim();
if (text !== "") {
todos.push(text);
inputtext.value = ""; // Clear input field
updateTodoList(); // Re-render UI
}
}
function removeTodo(i) {
todos.splice(i, 1);
updateTodoList(); // Re-render UI
}
function updateTodoList() {
todolist.innerHTML = ""; // Clear existing list before re-rendering
for (let i = 0; i < todos.length; i++) {
const t = todos[i];
const todo = `
<div class="w3-container w3-padding">
<div class="w3-card w3-padding w3-green">
Todo: ${i + 1} - ${t}
<button class="w3-right" onclick="removeTodo(${i})">x</button>
</div>
</div>`;
todolist.innerHTML += todo;
}
}
// Initial render
updateTodoList();
</script>
</body>
</html>State Diagram
Webinar Recording
The webinar recording will be available during the following week.
