i created a todolist and a button which removes checked tasks from an ul but it removes all li’s from an ul.
<div class="container mt-5">
<ul class="list-group" id="tasks"> </ul>
<div class="col">
<button type="button" class="btn btn-primary" id="delchecked">Delete done tasks</button>
</div>
const li=document.createElement("li");
li.className="list-group-item";
tasks.appendChild(li);
const checkBox=document.createElement("input")
checkBox.className="form-check-input me-1 fa-pull-right"
checkBox.type="checkbox"
checkBox.name="chk"
li.appendChild(checkBox);
checkBox.id="checkBox";
const delchecked=document.querySelector("#delchecked");
delchecked.addEventListener("click",deleteCheckedItem);
function deleteCheckedItem(){
var k=document.getElementsByName('chk');
for(var i=0; i<k.length; i++){
if(k[i].checked==true)
tasks.removeChild(li);
}
}
i want to remove selected li from an ul but code above removes all "li".
2
Answers
try this:
You should make your "create task" logic a function to demonstrate multiple tasks. In your example, you only create one task.
Now, if you want to delete a task, I advise you to find the nearest list relative to the button i.e.
closest
.