I have a function which lets the user add a element to a list onclick. Each element consist of a p element and a delete button:
function addToDo() {
//Get input value of ToDo
var getToDoValue = document.getElementById("toDoInput").value;
var toDoTaks = document.getElementById("toDoTasks");
//Create Element & set the value to userInput
var createToDoElement = document.createElement("p");
createToDoElement.classList.add("toDoElement");
createToDoElement.innerHTML = getToDoValue;
//Append to toDo
toDoTaks.appendChild(createToDoElement);
//Create Delete Button
var deleteButton = document.createElement("button");
deleteButton.innerHTML = "Delete";
toDoTaks.appendChild(deleteButton);
deleteButton.classList.add("deleteToDo");
}
This works fine. However, I want to create a new function which enables the user to click on one of the delete buttons which are created when "addToDo()" function is called, but I cant seem to get it to work.
I want to function to delete the clicked button and the above p element. This is what I got so far:
function myFunction() {
var test = document.querySelectorAll(".deleteToDo");
var test1 = document.querySelectorAll(".toDoElement")
test[0].remove();
test1[0].remove();
}
I know that I need to select in another way, and find a way to select the current cliked "delete" button. Im still new, so there are proberly some mistakes in the code.