§
Posted: 2020-11-16

for (var i = 0; i < document.querySelectorAll("#showQuickReply").length; i++) {
this.addEventListener("click", function(){ document.querySelectorAll("#quickReply")[i].style.display = ''; });
}

How can I pass the variable number 'i' to the quickReply element?
The script above works well, but the number i isn't added to the advent listener

wOxxOmMod
§
Posted: 2020-11-17

Either use let instead of var or use forEach or create a closure, more info: https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example/

§
Posted: 2020-11-20

Thanks, this worked.
But I didn't really understood how to implement this if I wanted to use forEach

wOxxOmMod
§
Posted: 2020-11-20
Edited: 2020-11-20

To use forEach with querySelectorAll the only additional trick is to convert it to an array:

[...document.querySelectorAll("#showQuickReply")].forEach((el, i) => { ....... })

i will be the index.

§
Posted: 2020-11-20

Thanks.
So it's the same as this
Array.from(document.querySelectorAll('a.Lightbox_AddEdit[class*="completed"]')).forEach(link => link.parentElement.parentElement.style.display = 'none');

Post reply

Sign in to post a reply.