Learn JavaScript

Lesson 4 of 9 · The DOM and Events: JavaScript Touches the Page

Module 14 · The DOM and Events: JavaScript Touches the Page

When to Use Which: Delegation, innerHTML, Data Attributes and Classes

Free with an accountReading

Rahim's shop page started small: ten products, ten "Add" buttons, ten listeners. A year later it shows five hundred products, loads more as you scroll, and filters as you type. New products appear without listeners. The basket total sometimes disagrees with the page. And a product named <Rahim's> Special once broke the layout.

Checking what this lesson needs for you…

What is inside When to Use Which: Delegation, innerHTML, Data Attributes and Classes

  1. Delegation or a listener per element
  2. textContent, innerHTML or building nodes
  3. A data attribute or a global variable
  4. A class or an inline style
  5. Keep a reference, or search again
  6. Which event: click, input, change or submit
  7. The comparison table