-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (48 loc) · 2.33 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
59
60
document.getElementById('add-btn').addEventListener('click', function() {
const taskInput = document.getElementById('todo-input');
const taskText = taskInput.value.trim();
if (taskText !== '') {
const listItem = document.createElement('li');
listItem.textContent = taskText;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', function() {
listItem.remove();
});document.getElementById('add-btn').addEventListener('click', function() {
const taskInput = document.getElementById('todo-input');
const taskText = taskInput.value.trim();
if (taskText !== '') {
const listItem = document.createElement('li');
listItem.textContent = taskText;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', function() {
listItem.remove();
});
listItem.appendChild(deleteButton);
document.getElementById('todo-list').appendChild(listItem);
taskInput.value = '';
} else {
alert('Please enter a task!');
}
});
// Function to save the to-do list as a text file
document.getElementById('save-btn').addEventListener('click', function() {
const tasks = document.querySelectorAll('#todo-list li');
let taskListText = '';
tasks.forEach(task => {
taskListText += task.firstChild.textContent + '\n';
});
const blob = new Blob([taskListText], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'todo-list.txt'; // Download file name
link.click();
});
listItem.appendChild(deleteButton);
document.getElementById('todo-list').appendChild(listItem);
taskInput.value = '';
} else {
alert('Please enter a task!');
}
});