💡 An editor is ready below the article—experiment freely!

Updated: May 31, 2026

Dom Event Delegation

📌

Conclusion

Dom Event Delegation — free browser-based JavaScript (DOM) practice

■ What this exercise teaches Listen for clicks on the element with id "list" and output the textContent of the clicked li with console.log.

Why it matters in real projects

Listening on the parent and checking e.target handles dynamically added children. Similar patterns appear daily in code review and production UI work.

Step-by-step workflow

1. Read the guide and understand the goal of "Dom Event Delegation" 2. Review the JavaScript (DOM) editor pane and start from the starter code 3. Open hints if stuck; compare your diff with the sample solution 4. Click Run and inspect preview/console output 5. Auto-check looks for output containing: addEventListener, target, LI. 6. After success, continue with adjacent exercises in the same category

Core concept explained

Listening on the parent and checking e.target handles dynamically added children.

Technical focus

Key JavaScript APIs: document.getElementById, console.log, addEventListener.

Hint before you peek at the solution

Use e.target to get the clicked element; check tagName for li.

Sample solution walkthrough

JavaScript APIs used: document.getElementById, console.log, addEventListener. Verify spelling and async/await usage. Explanation: Listening on the parent and checking e.target handles dynamically added children. Hint recap: Use e.target to get the clicked element; check tagName for li.

Solution code

📝 Sample code
document.getElementById('list')?.addEventListener('click', (e) => {
  if (e.target?.tagName === 'LI') console.log(e.target.textContent);
});

Common mistakes for this exact task

- Skipping the hint: Use e.target to get the clicked element; check tagName for li. - getElementById returns null if the id string differs by even one character

Troubleshooting

Q: Preview does not change after Run A: Ensure you edited the JavaScript (DOM) pane and clicked Run. Q: Looks correct but check fails A: Auto-check looks for output containing: addEventListener, target, LI. Look for typos, full-width spaces, or unclosed tags. Q: Console shows errors A: Open DevTools Console and fix bracket/quote mismatches at the reported line. Q: Hard to type on mobile A: Use landscape mode or continue on desktop—the same URL keeps progress in localStorage.

Interview / portfolio tips

- Explain the techniques in "Dom Event Delegation" in your own words within 30 seconds - Practice walking through each line of the sample solution on a whiteboard - Connect this task to a real component you would ship - Mention responsive or accessibility trade-offs - Describe when you use: document.getElementById, console.log, addEventListener

What to do after you pass

Move to the next exercise in the dom track, then continue the learning roadmap at /en/learning/js-basic/.

Keywords

Free hands-on exercise covering Event delegation, dom, intermediate, document.getElementById, console.log, addEventListener, dom event delegation—search-friendly guide for Dom Event Delegation.

🧪 Try it here — safe to break things

Use the editor below to write code and preview instantly.

Jump to editor

■ Exercise task

Listen for clicks on the element with id "list" and output the textContent of the clicked li with console.log.

Hint if stuck

Use e.target to get the clicked element; check tagName for li.

25pt

Event delegation

Listen for clicks on the element with id "list" and output the textContent of the clicked li with console.log.

Click error to highlight line. ✕=error ⚠=warning ›=log.

Roadmap · All guides

Deep dive

"Dom Event Delegation" is a intermediate-level JavaScript (DOM) exercise. Listen for clicks on the element with id "list" and output the textContent of the clicked li with console.log. Run it in-browser with automatic checking.

Listening on the parent and checking e.target handles dynamically added children. Similar patterns appear daily in code review and production UI work.

Pair this with the js-basic roadmap. Exercise #62 reinforces core dom skills.

Step-by-step workflow

  1. Read the guide and understand the goal of "Dom Event Delegation"
  2. Review the JavaScript (DOM) editor pane and start from the starter code
  3. Open hints if stuck; compare your diff with the sample solution
  4. Click Run and inspect preview/console output
  5. Auto-check looks for output containing: addEventListener, target, LI.
  6. After success, continue with adjacent exercises in the same category

How to read the sample solution

JavaScript APIs used: document.getElementById, console.log, addEventListener. Verify spelling and async/await usage. Explanation: Listening on the parent and checking e.target handles dynamically added children. Hint recap: Use e.target to get the clicked element; check tagName for li.

Real-world & career relevance

DOM APIs power clicks, forms, and modals—from jQuery era to modern SPAs.

Common mistakes

  • Skipping the hint: Use e.target to get the clicked element; check tagName for li.
  • getElementById returns null if the id string differs by even one character
  • Typo in getElementById
  • Using onClick instead of addEventListener
  • Setting textContent without null check

Troubleshooting

Preview does not change after Run
Ensure you edited the JavaScript (DOM) pane and clicked Run.
Looks correct but check fails
Auto-check looks for output containing: addEventListener, target, LI. Look for typos, full-width spaces, or unclosed tags.
Console shows errors
Open DevTools Console and fix bracket/quote mismatches at the reported line.
Hard to type on mobile
Use landscape mode or continue on desktop—the same URL keeps progress in localStorage.

Interview & portfolio tips

  • Explain the techniques in "Dom Event Delegation" in your own words within 30 seconds
  • Practice walking through each line of the sample solution on a whiteboard
  • Connect this task to a real component you would ship
  • Mention responsive or accessibility trade-offs
  • Describe when you use: document.getElementById, console.log, addEventListener

Glossary

DOM
Document Object Model—tree API for HTML.
getElementById
Fetch one element by id.
addEventListener
Register click and other events.

Keywords covered on this page

Free hands-on exercise covering Event delegation, dom, intermediate, document.getElementById, console.log, addEventListener, dom event delegation—search-friendly guide for Dom Event Delegation.

Related dev tools

Learning roadmap

Learning roadmap →

Related exercises

Frequently asked questions

Is "Event delegation" free?

This exercise is free and runs entirely in your browser. No account or credit card required.

Difficulty and time?

"Event delegation" is level "intermediate". Read the guide first; beginners can usually finish within 30 minutes. Allow 5–15 minutes per exercise. Use hints, then the solution if stuck.

What after a correct answer?

After a correct answer, use "Next exercise". When you finish a category, continue on the <a href="/en/learning/js-basic/">learning roadmap</a>.

Works on mobile?

Works on mobile browsers, but typing code is easier on a desktop.

Where are other exercises?

All exercises: <a href="/en/learn/">/en/learn/</a> with category filters.

Key takeaway for "Event delegation"?

Listening on the parent and checking e.target handles dynamically added children. Hint: Use e.target to get the clicked element; check tagName for li.

Where does DOM fit?

DOM APIs power clicks, forms, and modals—from jQuery era to modern SPAs.

Step-by-step for "Dom Event Delegation"?

1. Read the guide and understand the goal of "Dom Event Delegation"<br />2. Review the JavaScript (DOM) editor pane and start from the starter code<br />3. Open hints if stuck; compare your diff with the sample solution<br />4. Click Run and inspect preview/console output<br />5. Auto-check looks for output containing: addEventListener, target, LI.<br />6. After success, continue with adjacent exercises in the same category

How to read the sample solution?

JavaScript APIs used: document.getElementById, console.log, addEventListener. Verify spelling and async/await usage.<br /><br />Explanation: Listening on the parent and checking e.target handles dynamically added children.<br /><br />Hint recap: Use e.target to get the clicked element; check tagName for li.

Something is not working?

<strong>Preview does not change after Run</strong> → Ensure you edited the JavaScript (DOM) pane and clicked Run.<br /><strong>Looks correct but check fails</strong> → Auto-check looks for output containing: addEventListener, target, LI. Look for typos, full-width spaces, or unclosed tags.<br /><strong>Console shows errors</strong> → Open DevTools Console and fix bracket/quote mismatches at the reported line.<br /><strong>Hard to type on mobile</strong> → Use landscape mode or continue on desktop—the same URL keeps progress in localStorage.

Career / interview angle?

Explain the techniques in "Dom Event Delegation" in your own words within 30 seconds Practice walking through each line of the sample solution on a whiteboard Connect this task to a real component you would ship Mention responsive or accessibility trade-offs Describe when you use: document.getElementById, console.log, addEventListener

Related dev tools?

<a href="/en/tools/json-formatter/">JSON Formatter</a>, <a href="/en/tools/char-count/">Character Count</a>

Prerequisites?

Read the <a href="/en/learning/js-basic/">Learning roadmap</a> chapter first.

Can I change the starter code?

Yes—if it works. Understanding the starter first, then adding minimal diffs, speeds up learning.

What do the points mean?

25pt is a difficulty hint, not a score you must maximize.