Skip to content

Commit fa5abfb

Browse files
Merge pull request #4 from laxman-goud/fix/empty-todo-bug
Fix: Prevent adding empty todo items (#3)
2 parents b386e1a + bdb553e commit fa5abfb

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

20-SessionStorage-TodoList/app.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ function get_todos() {
99
}
1010

1111
function add() {
12-
var task = document.getElementById("task").value;
12+
var task = document.getElementById("task").value.trim(); // remove extra spaces
13+
var alertMessage = document.getElementById("alert");
14+
alertMessage.style.display = "none"; // hiding alert message initially
15+
16+
if (task === "") {
17+
alertMessage.style.display = "block"; // show alert if task is empty
18+
return false; // stop if empty
19+
}
1320

1421
var todos = get_todos();
1522
todos.push(task);
1623
localStorage.setItem("todo", JSON.stringify(todos));
1724

1825
show();
1926
clearDefault();
20-
return false; //avoids any futher action with click event
27+
return false; // avoids any further action with click event
2128
}
2229

30+
2331
//clear the task value from input box
2432
function clearDefault() {
2533
document.getElementById("task").value = "";

20-SessionStorage-TodoList/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<body>
1111
<div id="wrapper">
1212
<h1>Todo List</h1>
13+
<p id="alert">Task field is empty</p>
1314
<input type="" id="task" /><button id="add">Add Task</button>
1415
<div id="todos"></div>
1516
</div>

20-SessionStorage-TodoList/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,10 @@ ul {
5353
h1 {
5454
margin-top: 30px;
5555
}
56+
57+
#alert {
58+
display: none;
59+
color: red;
60+
font-weight: bold;
61+
margin-top: 10px;
62+
}

0 commit comments

Comments
 (0)