#Why am I getting "PointerEvent" when I click an li from a for loop and event listener?
18 messages · Page 1 of 1 (latest)
I am not getting an error, but I am not getting what I want, I want to get the html itself
then in your anonymous closure you're creating, you need to capture the element you're adding the event listener to, or listLi[i]
as you can see, the argument passed to your anonymous function is the PointerEvent
I am getting the same result when I console.log(listLi[i])
are you sure?
but also, i think i will change, so you need to capture that value
so not hte same 🙂
What do you mean by capture?
the problem is that by the time the event listener fires, i has changed. and then probably listLi has changed too
I understand the logic of the word 'capture', but I am not sure how I would do that
it's been a while since I did this, but one way would be with a .bind() call on your anonymous function
something like:
const handler = (function(event) {
console.log(this);
}).bind(listLi[i]);
listLi[i].addEventListener("click", handler);
Thank you, this works well and makes sense
what you are printing is the event, you only need to add .target to your item
like:
console.log(item.target);
but as i see you found out a method
ìzem.target is the HTMLelement so you can than work with it as usually:
item.target.style = "your amazing styles", etc.