skip to content

DOM and Events

You will learn how the browser models a document as a live tree of nodes and how events travel through that tree. This is the oldest and most reliably asked part of frontend interviewing, because every framework's virtual DOM and synthetic event system is a wrapper over these exact rules.

on this pageshow

questions

page 2 of 2

Besides a function, addEventListener accepts an object as its listener argument. What must that object provide, and what is `this` inside it when the event fires?

level: middleimportance: nice to knowfreq 16%

basics

~10 s

The object must have a handleEvent method. The browser calls listener.handleEvent(event), and inside that method this is the listener object itself, not the element. Pass the same object to removeEventListener to unregister it.

open as a page

You need a framework-free way for a plain JavaScript service object — a data store, not a DOM node — to notify subscribers. What does `class Store extends EventTarget` give you, and what does it not?

level: seniorimportance: nice to knowfreq 34%

basics

~20 s

Extending EventTarget gives any object the standard addEventListener, removeEventListener and dispatchEvent methods, including listener options such as once and signal. It gives no propagation, no listener introspection, and no way to know whether anyone subscribed.

open as a page

During a DOM click dispatch, a handler near the target removes the clicked element from the document. Why do listeners on its former ancestors still run, and what do event.target.closest() and event.composedPath() each return after that removal?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

The browser computes the propagation path once, when dispatch begins, so removing the target mid-dispatch does not reroute the event and ancestor listeners still run. closest() then walks the detached node's remaining ancestors and can return null, while composedPath() returns the path recorded at dispatch.

open as a page

A legacy page renders with subtly wrong element widths, and document.compatMode reports "BackCompat". What has the browser done, what in the HTML caused it, and can you switch it at runtime?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

"BackCompat" means the browser parsed the page in quirks mode and is applying legacy layout rules, including the old box model where width includes padding and border. A missing or unrecognised DOCTYPE causes it, and the mode is fixed at parse time.

open as a page

You take a node out of a same-origin iframe with iframe.contentDocument.querySelector('.row') and append it into the parent page. What happens to the node's ownerDocument, and why can `row instanceof HTMLElement` be false in the parent page?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

Inserting the node adopts it: the browser changes its ownerDocument to the parent page's document and the append succeeds. Adoption does not change the object's prototype, which still comes from the iframe's realm, so instanceof against the parent window's HTMLElement fails.

open as a page

How do you decide which selectors a shared widget's JavaScript uses to find its own DOM nodes, so that a CSS or markup refactor cannot silently break its behaviour?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Give behaviour its own stable hooks — dedicated data-* attributes rather than styling class names — scope every query to the widget root, avoid structural selectors, and fail loudly when a required node is missing instead of silently doing nothing.

open as a page

showing 31–36 of 36