-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (38 loc) · 1.38 KB
/
script.js
File metadata and controls
50 lines (38 loc) · 1.38 KB
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 todoInput = document.getElementById('todoInput');
const addTodoBtn = document.getElementById('addToDoBtn');
const todoList = document.getElementById('todoList');
addTodoBtn.addEventListener('click', addTodo);
todoInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
addTodo();
}
});
function addTodo() {
const todoText = todoInput.value.trim();
if (todoText !== '') {
const li = document.createElement('li');
const textSpan = document.createElement('span');
textSpan.textContent = todoText;
const editBtn = document.createElement('button');
editBtn.textContent = 'Edit';
editBtn.classList.add('edit-btn');
editBtn.addEventListener('click', function() {
const newText = prompt('Edit your todo', textSpan.textContent);
if (newText !==null) {
textSpan.textContent = newText.trim();
}
});
const removeBtn = document.createElement('button');
removeBtn.textContent = 'Remove';
removeBtn.classList.add('remove-btn');
removeBtn.addEventListener('click', function(){
li.remove();
});
li.appendChild(textSpan);
li.appendChild(editBtn);
li.appendChild(removeBtn);
todoList.appendChild(li);
todoInput.value = '';
todoInput.focus();
}
}