When my page changes, the eventlisteners of elements with a certain classname should be added. There may be multiple Elements with this classname. The elements also have an attribute called taskNr
. When clicked, the eventlistener function should delete an element with the id "task" + taskNr of the Element, the eventlistener is attached to.
I have tried to give the function a Parameter with the taskNr
, but it changes to the taskNr
of the last Element. That is why i now think about referring to taskNr
from inside the function.
//fügt eventlistener für button removetask hinzu
function activateremovetasklistener(){
var removeBtns = document.getElementsByClassName("fa fa-times-circle floatright fa-fw notfirst");
for (i=0; i < removeBtns.length; i++) {
var taskNr = removeBtns[i].getAttribute("taskNr");
removeBtns[i].addEventListener("click", function(){
removeTask(taskNr);
})
};
}
//entfernt Task
function removeTask(tasknumber){
var taskClass = document.getElementById("task" + tasknumber);
taskClass.remove();
}
With this code it uses always the taskNr
of the last element in removeBtns
. It should use the one from removeBtns[i]
(the element the eventlistener is attached to).