Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete button functionality #294

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions ToDo JS Extension/scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ function showTasks(){
}
let newLiTag = "";
listArray.forEach((element, index) => {
newLiTag += `<li>${element}<span class="icon delete-btn"><i class="fas fa-trash"></i></span></li>`;
newLiTag += `<li data-index="${index}">${element}<span class="icon delete-btn"><i class="fas fa-trash"></i></span></li>`;
});
todoList.innerHTML = newLiTag; //adding new li tag inside ul tag
inputBox.value = ""; //once task added leave the input field blank
}
//event delegation
// Event delegation for delete button
todoList.addEventListener("click", function(event) {
if (event.target.classList.contains("delete-btn")) {
let index = event.target.dataset.index;
const target = event.target;
// Check if the click event targets the delete button icon directly
if (target.classList.contains("fa-trash")) {
// If so, retrieve the index of the todo item from its parent <li> element
const index = target.closest("li").dataset.index;
deleteTask(index);
} else if (target.classList.contains("delete-btn")) {
// If the click event targets the delete button container (orange square),
// retrieve the index of the todo item from the closest parent <li> element
const index = target.closest("li").dataset.index;
deleteTask(index);
}
});
Expand Down
Loading