-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
46 lines (39 loc) · 1.1 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
let tasks = [];
function renderTasks() {
const taskList = document.getElementById('taskList');
taskList.innerHTML = '';
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.classList.add('task-item');
li.innerHTML = `
<span>${task}</span>
<button class="edit-btn" onclick="editTask(${index})">Edit</button>
<button class="delete-btn" onclick="deleteTask(${index})">Delete</button>
`
;
taskList.appendChild(li);
});
}
function addTask() {
const taskInput = document.getElementById('taskInput');
const task = taskInput.value.trim();
if (task !== '') {
tasks.push(task);
renderTasks();
taskInput.value = '';
} else {
alert('enter a valid task!');
}
}
function deleteTask(index) {
tasks.splice(index, 1);
renderTasks();
}
function editTask(index) {
const newTask = prompt('Edit Task:', tasks[index]);
if (newTask !== null) {
tasks[index] = newTask.trim();
renderTasks();
}
}
//window.onload = renderTasks;