JavaScript Curriculum
MutationObserver & ResizeObserver
mediumThe coffee shop admin panel uses a third-party widget that modifies the DOM unpredictably. The sidebar needs its own responsive breakpoints independent of the window width. Both problems are solved by the Observer APIs.
MutationObserver & ResizeObserver
The third Observer in the browser toolkit. MutationObserver watches the DOM tree for changes. ResizeObserver watches elements for size changes. Both are async, performant, and replace fragile polling or resize event hacks.
MutationObserver — Watch the DOM Change
Observe DOM mutations in real time — add children, change attributes, modify text:
<p class="intro">Hello <strong>World</strong></p>
const mo = new MutationObserver(
(mutations) => {
mutations.forEach(m => {
console.log(m.type)
// 'attributes'
// 'childList'
// 'characterData'
})
}
)
mo.observe(el, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})ResizeObserver — Element-Level Breakpoints
Watch individual elements resize — not the window:
// Drag the handles to resize
const ro = new ResizeObserver((entries) => {
entries.forEach(entry => {
const { width, height } = entry.contentRect
// Component-level breakpoints:
if (width < 240) applyLayout('mobile')
else if (width < 360) applyLayout('tablet')
else applyLayout('desktop')
})
})
ro.observe(document.querySelector('.card-grid'))
// No window resize listener needed!All Three Observers Compared
When to reach for each observer:
Watches the DOM for changes — added/removed nodes, attribute changes, text content changes.
- ›Detect when a third-party script modifies the DOM
- ›Auto-save form content when any input changes
- ›Watch for dynamically added elements to wire up behaviour
- ›Implement undo/redo by recording DOM mutations
const mo = new MutationObserver((mutations) => {
mutations.forEach(m => {
if (m.type === 'childList') {
m.addedNodes.forEach(node => {
if (node.nodeType === 1) {
wireUpNode(node)
}
})
}
})
})
mo.observe(document.body, {
childList: true, subtree: true
})Your Challenge
On .cards container, create a MutationObserver with { childList: true }. When a card is added, log its h3 text. Create a ResizeObserver on .sidebar — toggle class compact when width < 250px. Both should disconnect() when the page navigates away.
Challenge
Wire a MutationObserver on the .cards container to auto-wire click handlers on any newly added cards. Wire a ResizeObserver on a .sidebar element that adds class 'compact' when its width drops below 250px.
MutationObserver & ResizeObserver
mediumThe coffee shop admin panel uses a third-party widget that modifies the DOM unpredictably. The sidebar needs its own responsive breakpoints independent of the window width. Both problems are solved by the Observer APIs.
MutationObserver & ResizeObserver
The third Observer in the browser toolkit. MutationObserver watches the DOM tree for changes. ResizeObserver watches elements for size changes. Both are async, performant, and replace fragile polling or resize event hacks.
MutationObserver — Watch the DOM Change
Observe DOM mutations in real time — add children, change attributes, modify text:
<p class="intro">Hello <strong>World</strong></p>
const mo = new MutationObserver(
(mutations) => {
mutations.forEach(m => {
console.log(m.type)
// 'attributes'
// 'childList'
// 'characterData'
})
}
)
mo.observe(el, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})ResizeObserver — Element-Level Breakpoints
Watch individual elements resize — not the window:
// Drag the handles to resize
const ro = new ResizeObserver((entries) => {
entries.forEach(entry => {
const { width, height } = entry.contentRect
// Component-level breakpoints:
if (width < 240) applyLayout('mobile')
else if (width < 360) applyLayout('tablet')
else applyLayout('desktop')
})
})
ro.observe(document.querySelector('.card-grid'))
// No window resize listener needed!All Three Observers Compared
When to reach for each observer:
Watches the DOM for changes — added/removed nodes, attribute changes, text content changes.
- ›Detect when a third-party script modifies the DOM
- ›Auto-save form content when any input changes
- ›Watch for dynamically added elements to wire up behaviour
- ›Implement undo/redo by recording DOM mutations
const mo = new MutationObserver((mutations) => {
mutations.forEach(m => {
if (m.type === 'childList') {
m.addedNodes.forEach(node => {
if (node.nodeType === 1) {
wireUpNode(node)
}
})
}
})
})
mo.observe(document.body, {
childList: true, subtree: true
})Your Challenge
On .cards container, create a MutationObserver with { childList: true }. When a card is added, log its h3 text. Create a ResizeObserver on .sidebar — toggle class compact when width < 250px. Both should disconnect() when the page navigates away.
Challenge
Wire a MutationObserver on the .cards container to auto-wire click handlers on any newly added cards. Wire a ResizeObserver on a .sidebar element that adds class 'compact' when its width drops below 250px.