-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (42 loc) · 1.36 KB
/
script.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
43
44
45
46
47
48
49
50
const todoList = [];
renderTodoList();
function renderTodoList(){
let todoListHtml = '';
todoList.forEach(function(todoObject,index){
const {name,dueDate} = todoObject;
const html = `
<div>${name}</div>
<div>${dueDate} </div>
<button class="delete-todo-button js-delete-todo-button">Delete</button>
`;
todoListHtml += html;
});
document.querySelector('.js-todo-list')
.innerHTML = todoListHtml;
// delete todo button eventlistener
document.querySelectorAll('.js-delete-todo-button')
.forEach((deleteButton, index) => {
deleteButton.addEventListener('click', () => {
todoList.splice(index,1);
renderTodoList();
});
})
}
// add todo button eventlistener
document.querySelector('.js-add-todo-button')
.addEventListener('click',() => {
addTodo();
})
function addTodo(){
const inputElement = document.querySelector('.js-name-input');
const name = inputElement.value;
const dateInputElement = document.querySelector('.js-due-date-input');
const dueDate = dateInputElement.value;
todoList.push({
name,
dueDate
});
inputElement.value = '';
dateInputElement.value = '';
renderTodoList();
}