JS-function on array
Hey so, I've got an array and I need a listener on it that'll always be listening
It has to run a function, using the index of the element within the array its been clicked on (As it would have to delete the element including its siblings and its parentElement
However, I cannot put an addEventListener on the raw array it seems, this is what I've got:
As a sidenote, this function will have an array of [] in the beginning, as no elements will have that className yet (I figured to use getElementsByClassName as its dynamic and not static and the array can get longer and shorter, if one is deleted obv it'll get shorter if ones added itll get longer)
The error in the console is:
16 Replies
you have to loop over the array:
oh I see and they'll always be listening then?
the elements that have been selected when
document.getElementsByClassName("removeSelector")
ran, will have those event listeners on them yes
if you later add elements with removeSelector
on them, those won't have event listeners. You have to add those when you add the elementhmm, okay then I think I'll add a run everytime when is added
i already have the code that adds one
ill just add a run there
just make sure to not re-add the listener to any element that already has it on there
so don't just rerun
document.getElementsByClassName("removeSelector")
and loop over that resultHmm, so what if I add the listener on creation of the element?
instead of on the entire array
add it when the element is being created
would that do the trick?
yeah, that's usually how I do it
alright I'll see how that goes in a moment
Alright it worked, I've added them on creation and they delete the entire family including parent
As quick question regarding what I asked about in #Reformulating + check for if correct , this would have been more difficult if I were to use arrow syntax as I can't call
this.parentElement
when using arrow syntax, am I correct in this assumption?
I'm asking this to ensure that I fully understand the key differences in function() & =>you're right you wouldn't be able to use
this
, but you can use event.currentTarget
instead
https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTargetand that's basically the same as
this
then, I assume
So theoretically speaking, it doesn't matter whether you use function() or =>
The only difference is how to call itself (but both are possible)
and the fact that you have to use them on declare and cannot be used freely like a function() is able toInstead of having an event listener on each element, it's better to have one listener on the element's parent so even if you add/remove elements you don't have to worry about adding/removing listeners. It's called event delegation.
how did you put that text into a link?
[Link text](<https://link.url>)
that's handy
(be sure to have the URL inside
<>
otherwise Discord will do an embed:
event delegation.
Oh, I guess not. They fixed thatAlright, I'll go edit that part of the code now, having 1 listener instead of multiple seems like a good idea :p