Hopefully this helps you visually the concept behind event delegation and convinces you of delegation's power! Cohesion already existed; these buttons are toolbar buttons, and when you click on, a centralized handler will determine what happens. Imagine a use case where you have a grid of several hundred dynamically generated table cells think an hour-by-hour availability calendar : attaching an event to each of these cells would, for starters, take a moment. So… Is this some venerated yet anachronistic style, or are there any statistics or tests I can run that show the benefits. When the event listener is triggered, check the event element to ensure it's the type of element to react to. Is there a performance improvement done by using your method? That will take time, then you have to loop again to apply the new listeners. Adding and removing event listeners would be a nightmare, especially if addition and removal code is in different places within your app. You just have to compare the node instances… right? This is how you already code presumably , the real differences is that without delegation, you have more references to keep track of.
From a coding style perspective, event delegation shows odd cohesion. Your function references are still sitting in memory, too. When coding event handling in other environments like desktop, etc. If it's not an element that we want, the event can be ignored. Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent.
No matter what change takes place, the single event listener will bubble up any actions. But if you add the event listener to the parent, how will you know which element was clicked? There are indeed direct benefits. Let's try something more difficult. } ; Hi, this is an old post but still up to date. I just to add something that might be useful xD document.
} When className returns an empty string, split should return an array containing one empty string, which evaluates to true in an if statement. . . . . .
. . . . . . .
. . . . . .
. . . . . . .