-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 loc) · 896 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// list of todos
let todos = [];
// get all dom
const submit = document.getElementById("submit");
const output = document.getElementById("output");
const input = document.getElementById("input");
// display all todos
function displayTodos() {
let list = "";
todos.forEach(function(item, index) {
list += item + ` <button onclick="deleteTodo(${index})">X</button><br>`;
});
output.innerHTML = list;
}
// add new todo and display it
function addTodo() {
const todo = input.value;
todos.push(todo);
input.value = "";
displayTodos();
}
// delete a todo and display it
function deleteTodo(index) {
todos.splice(index, 1);
displayTodos();
}
// add todo if enter is clicked
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
addTodo();
}
});
// add todo if button is clicked
submit.addEventListener("click", function() {
addTodo();
});