Efficient Techniques to Build Clean, Scalable, and Performant UI
In the world of modern frontend development, performance, maintainability, and code efficiency are key. When building interactive web interfaces, two powerful JavaScript concepts come to the rescue:
-
Event Delegation
-
DOM Manipulation
Together, they form the backbone of dynamic user interfaces β especially when dealing with large lists, forms, modals, or SPA components.
In this blog, we'll explore both concepts in detail, with real-world examples and tips to use them effectively.
π§ What is the DOM in JavaScript?
DOM (Document Object Model) is the in-memory representation of your HTML structure, which JavaScript can access, manipulate, and interact with dynamically.
Through the DOM API, JavaScript can:
-
Add, remove, or change elements
-
Read or write attributes and text
-
Attach event listeners to respond to user actions
π οΈ Basic DOM Manipulation in JavaScript
Letβs start with simple operations you can do with DOM:
β Select Elements
β Create Elements
β Modify Attributes or Classes
β Remove Elements
π― What is Event Delegation?
Event Delegation is a technique where you attach a single event listener to a parent element, instead of adding multiple listeners to child elements.
The event "bubbles up" from the child to the parent, and you catch it on the way up.
π§ͺ Why Use It?
-
Better performance (fewer event listeners)
-
Cleaner code
-
Handles dynamically added elements automatically
π Real-Life Example: Without vs With Event Delegation
β Without Delegation:
Problems:
-
Doesnβt work for dynamically added buttons
-
Memory-heavy in large lists
β With Event Delegation:
Benefits:
-
One listener for all current and future
.delete-btns -
More scalable and maintainable
βοΈ How Event Delegation Works (Under the Hood)
When an event (like click) is triggered on an element, it bubbles up the DOM tree, triggering handlers on parent elements as it goes.
JavaScript allows us to catch this event at a higher level, check the target (e.target), and act accordingly.
π§ Key Properties Youβll Use
| Property | Description |
|---|---|
e.target | The actual element that triggered the event |
e.currentTarget | The element the event is currently on |
e.stopPropagation() | Stops the bubbling process (use with care) |
e.preventDefault() | Prevents default behavior (like form submission) |
π§© DOM Manipulation + Delegation = Dynamic UIs
Letβs build a dynamic todo list that adds and deletes items using both concepts:
π§ͺ Example: Todo List with Delegation
π Now you can add and remove todos without attaching multiple listeners!
π§ Best Practices for DOM & Event Delegation
β
Always delegate to a static parent element
β
Check e.target with .classList.contains()
β
Use event delegation for lists, tables, dynamic UIs
β
Avoid nesting too many listeners β it affects performance
β
Combine delegation with dataset-* attributes for better control
π§ Use Cases of Event Delegation in Real Projects
-
Todo apps
-
Dropdown menus
-
Accordions or tab navigation
-
Comment or chat lists
-
Dynamic table rows (e.g., delete/edit buttons)
-
Image galleries
β Final Thoughts
JavaScript's Event Delegation and DOM Manipulation are essential tools for building modern, dynamic web interfaces. They make your code:
-
Faster by reducing memory usage
-
Scalable for large UIs
-
Cleaner and easier to maintain
Whether youβre using vanilla JavaScript or planning to migrate to a framework like React or Vue β understanding these concepts will level up your frontend skills.