I asked if that statement was true and was given this information:
No, consider the example where you create an element inside the loop, imagine this as a grid- you use a loop to add divs to it and attach an event listener to those div. The event handler is being attached inside the loop. And this is quite common in my experience.
Besides, objectively stating this as a fact is bad. I mean, attaching event handler inside a loop to a single element would be bad but you cannot still state it objectively. Consider the weird scenario where you have got array of events or event listener callback, if you iterate over the array and attach it to a single element it can make up for a good use case.
In response to that someone said this:
imagine this as a grid- you use a loop to add divs to it and attach an event listener to those div.
Why not just attach a single event listener to the root element of the grid and use event delegation?
The reason this is usually considered bad practice is it’s generally more efficient to attach a single event listener to a parent element and check the event’s target property. This is called “event delegation”. Here are some resources you can read about this technique: