2 ms·
Sorry, no time right now for that. I'll try to explain more clearly. There's no problem if I can bind directly to the element. However, that's often not possibl
by mercer 10y ago
Sorry, no time right now for that. I'll try to explain more clearly. There's no problem if I can bind directly to the element. However, that's often not possible. Imagine a <Carousel> element that is initialized, event listeners and all, but only receives data (<Item> elements) at a later time.
In jQuery I can do something like this:
$(<Carousel>).on('click', <Item>, function(e) {
// using this 'this' gives you the <Item> element. Always. Even
// if you click on a child element of <Item>. And it works if
// you add or remove items. Also it doesn't add a listener
// to *every* single item, which matters if you've got a whole
// bunch of them!
});
The vanilla equivalent is often presented to be as simple as:
<Carousel>.addEventListener('click', function(e) {
// e.target is the actual element you clicked. So now we need to
// figure out if this element is inside an <Item>, and perhaps
// also *which* <Item>. There's various approaches, and it's
// not rocket science, but it's all extra work for something that
// seems like a common scenario.
});