Created
February 19, 2020 08:30
-
-
Save mburakerman/41499fac1beabf7ad729606fca4067f6 to your computer and use it in GitHub Desktop.
svelte todo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
$: todos = []; | |
var todo = ""; | |
function addTodoOnEnter(e) { | |
if (e.key === "Enter") { | |
addTodo(); | |
} | |
} | |
function addTodo() { | |
if (todo.length > 1) { | |
todos.push(todo); | |
todo = ""; | |
todos = todos; | |
} | |
} | |
function removeTodo() { | |
var clickedTodo = this.parentElement; | |
var clickedTodoIndex = [...document.querySelectorAll("li")].indexOf( | |
clickedTodo | |
); | |
var filteredTodos = todos.filter(function(todoItem, todoIndex) { | |
return todoIndex != clickedTodoIndex; | |
}); | |
todos = filteredTodos; | |
} | |
</script> | |
<style> | |
</style> | |
<main> | |
<input type="text" bind:value={todo} on:keyup={addTodoOnEnter} /> | |
<button on:click={addTodo}>add todo</button> | |
<ul> | |
{#each todos as t} | |
<li> | |
{t} | |
<small on:click={removeTodo}>remove</small> | |
</li> | |
{/each} | |
</ul> | |
</main> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment