-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (45 loc) · 1.49 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
51
52
53
54
55
56
57
58
// Get elements from the DOM
const taskForm = document.getElementById('taskForm');
const taskInput = document.getElementById('taskInput');
const taskList = document.getElementById('taskList');
// Initialize tasks array
let tasks = [];
// Function to render tasks in the UI
function renderTasks() {
// Clear the task list
taskList.innerHTML = '';
// Render each task as a list item
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.textContent = task;
li.innerHTML += `<span class="delete-btn" data-index="${index}">X</span>`;
taskList.appendChild(li);
});
// Add event listeners to delete buttons
const deleteButtons = document.getElementsByClassName('delete-btn');
Array.from(deleteButtons).forEach((button) => {
button.addEventListener('click', deleteTask);
});
}
// Function to add a new task
function addTask(event) {
event.preventDefault();
// Get the task input value
const newTask = taskInput.value;
// Add the task to the tasks array
tasks.push(newTask);
// Render the updated tasks
renderTasks();
// Clear the input field
taskInput.value = '';
}
// Function to delete a task
function deleteTask() {
const index = parseInt(this.getAttribute('data-index'));
// Remove the task from the tasks array
tasks.splice(index, 1);
// Render the updated tasks
renderTasks();
}
// Event listener for form submission
taskForm.addEventListener('submit', addTask);