Skip to content

Commit f96d991

Browse files
Changed the code to add a 'empty field alert'
1 parent 32858f9 commit f96d991

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Sprint-3/todo-list/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
/>
2727
</div>
2828
<div>
29-
<button type="submit" class="btn btn-primary mt-2">Add Todo</button>
29+
<button type="button" id="add-btn" class="btn btn-primary mt-2">
30+
Add Todo
31+
</button>
3032
<button
3133
type="button"
3234
id="remove-all-completed"

Sprint-3/todo-list/script.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,23 @@ function addNewTodo(event) {
6060
let input = document.getElementById("todoInput");
6161
let newTask = input.value.trim();
6262

63+
if (newTask === "") {
64+
alert("Input field cannot be empty.");
65+
return;
66+
}
67+
6368
if (/[^A-Za-z\s]/.test(newTask)) {
6469
alert("Please enter letters and spaces only.");
6570
input.value = "";
6671
return;
6772
}
6873

69-
if (newTask !== "") {
70-
todos.push({ task: newTask, completed: false });
71-
populateTodoList(todos);
72-
input.value = "";
73-
}
74+
todos.push({ task: newTask, completed: false });
75+
populateTodoList(todos);
76+
input.value = "";
7477
}
7578

76-
document.getElementById("todo-form").addEventListener("submit", addNewTodo);
79+
document.getElementById("add-btn").addEventListener("click", addNewTodo);
7780
document
7881
.getElementById("remove-all-completed")
7982
.addEventListener("click", deleteAllCompletedTodos);

0 commit comments

Comments
 (0)