diff --git a/javascript/event-delegation.md b/javascript/event-delegation.md new file mode 100644 index 0000000..585a95d --- /dev/null +++ b/javascript/event-delegation.md @@ -0,0 +1,50 @@ +# Event Delegation in JavaScript + +How can we improve the performance of this code? + +```html + + + +``` + +Instead of adding an event listener to _each_ list element, we can use “event delegation” to add a single event listener to the parent element `(ul)` and check if the event target is a list item. This has a few advantages: + +_Memory Efficiency:_ Event delegation conserves memory by reducing the number of event listeners, since one parent listener replaces multiple child listeners. + +_Handling Dynamic Element:_ Event delegation smoothly manages dynamically added or removed elements, as the parent listener remains consistent, without needing to re-bind. + +```html + + + +``` + +[source](https://bytes.dev/archives/189)