Skip to main content

A page that only has a single action is, in practice, quite rare. Most of the time you’ll need to have multiple actions on a page. In this app, creating a todo isn’t enough — we’d like to delete them once they’re complete.

Begin by replacing our default action with named create and delete actions:

src/routes/+page.server
export const actions = {
	create: async ({ cookies, request }) => {
		const data = await request.formData();
		db.createTodo(cookies.get('userid'), data.get('description'));
	},

	delete: async ({ cookies, request }) => {
		const data = await request.formData();
		db.deleteTodo(cookies.get('userid'), data.get('id'));
	}
};

Default actions cannot coexist with named actions.

The <form> element has an optional action attribute, which is similar to an <a> element’s href attribute. Update the existing form so that it points to the new create action:

src/routes/+page
<form method="POST" action="?/create">
	<label>
		add a todo:
		<input
			name="description"
			autocomplete="off"
		/>
	</label>
</form>

The action attribute can be any URL — if the action was defined on another page, you might have something like /todos?/create. Since the action is on this page, we can omit the pathname altogether, hence the leading ? character.

Next, we want to create a form for each todo, complete with a hidden <input> that uniquely identifies it:

src/routes/+page
<ul class="todos">
	{#each data.todos as todo (todo.id)}
		<li>
			<form method="POST" action="?/delete">
				<input type="hidden" name="id" value={todo.id} />
				<span>{todo.description}</span>
				<button aria-label="Mark as complete"></button>
			</form>
		</li>
	{/each}
</ul>

Edit this page on GitHub

<script>
let { data } = $props();
</script>

<div class="centered">
<h1>todos</h1>

<form method="POST">
<label>
add a todo:
<input
name="description"
autocomplete="off"
/>
</label>
</form>

<ul class="todos">
{#each data.todos as todo (todo.id)}
<li>
{todo.description}
</li>
{/each}
</ul>
</div>

<style>
.centered {
max-width: 20em;
margin: 0 auto;
}

label {
width: 100%;
}

input {
flex: 1;
}

span {
flex: 1;
}

button {
border: none;
background: url(./remove.svg) no-repeat 50% 50%;
background-size: 1rem 1rem;
cursor: pointer;
height: 100%;
aspect-ratio: 1;
opacity: 0.5;
transition: opacity 0.2s;
}

button:hover {
opacity: 1;
}

.saving {
opacity: 0.5;
}
</style>


VITE v5.4.3 ready in 2447 ms

Local: http://localhost:5173/
Network: use --host to expose
press h + enter to show help
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:44:1 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:45:2 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:48:1 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:04 PM [vite-plugin-svelte] src/routes/+page.svelte:41:1 Unused CSS selector "span"
8:05:04 PM [vite-plugin-svelte] src/routes/+page.svelte:45:1 Unused CSS selector "button"
8:05:04 PM [vite-plugin-svelte] src/routes/+page.svelte:56:1 Unused CSS selector "button:hover"
8:05:04 PM [vite-plugin-svelte] src/routes/+page.svelte:60:1 Unused CSS selector ".saving"
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:44:1 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:45:2 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:04 PM [vite-plugin-svelte] .svelte-kit/generated/root.svelte:48:1 `<svelte:component>` is deprecated in runes mode — components are dynamic by default
8:05:06 PM [vite-plugin-svelte] src/routes/+page.svelte:41:1 Unused CSS selector "span"
8:05:06 PM [vite-plugin-svelte] src/routes/+page.svelte:45:1 Unused CSS selector "button"
8:05:06 PM [vite-plugin-svelte] src/routes/+page.svelte:56:1 Unused CSS selector "button:hover"
8:05:06 PM [vite-plugin-svelte] src/routes/+page.svelte:60:1 Unused CSS selector ".saving"