-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (26 loc) · 956 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
document.addEventListener('DOMContentLoaded', () => {
const newTaskInput = document.getElementById('new-task')
const addTaskBtn = document.getElementById('add-task-btn')
const taskList = document.getElementById('task-list')
const addTask = () => {
const taskText = newTaskInput.value.trim()
if (taskText != '') {
const li = document.createElement('li');
li.innerHTML = `<span>${taskText}</span> <button class="delete-btn">Delete</button>`;
taskList.appendChild(li)
li.querySelector('.delete-btn').addEventListener('click', () => {
li.remove()
})
li.addEventListener('click', () => {
li.classList.toggle('completed')
})
newTaskInput.value = '';
}
}
addTaskBtn.addEventListener('click', addTask)
newTaskInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
addTask()
}
})
})