Collection · 30 utilities

DOM utilities

Elements, geometry, focus, scrolling and forms. The geometry checks run in the lab; the rest run in Chromium against the markup each page shows.

Check if two DOMRects intersect

Two rects intersect when they meet on both axes.

function intersect({ a, b }) { return b.x <= a.x + a.width && b.x + b.width >= a.x && b.y <= a.…DOM

Get the center point of a DOMRect

Return [x, y] of the rect’s center.

function getCenter({ x, y, width, height }) { return [x + width / 2, y + height / 2]; }DOM

Check if an element has CSS overflow

True when either axis is styled to scroll: overflow auto or scroll.

function hasCssOverflow(element) { const { overflowX, overflowY } = getComputedStyle(element); …DOM

Check if an element has overflow

True when the content is larger than the element’s visible box.

function hasOverflow(element) { return element.scrollHeight > element.clientHeight || element.s…DOM

Check if an element is focused

Compare the element with document.activeElement.

function hasFocus(element) { return element === document.activeElement; }DOM

Check if the touch events are supported

True when the browser exposes touch events or reports touch points.

function touchSupported() { return 'ontouchstart' in window || navigator.maxTouchPoints > 0; }DOM

Detect Internet Explorer browser

Look for document.documentMode, which only Internet Explorer defines.

function isInternetExplorer() { return 'documentMode' in document; }DOM

Detect macOS browser

Guess whether the device is a Mac, iPhone or iPad from the platform string.

function isApplePlatform() { return /Mac|iPhone|iPad|iPod/.test(navigator.userAgentData?.platfo…DOM

Get all siblings of an element

Return the other element children of the element’s parent.

function siblings(element) { return element.parentElement ? [...element.parentElement.children]…DOM

Get the selected text

Read the text of the current document selection.

function getSelectedText() { return window.getSelection()?.toString() ?? ''; }DOM

Hide an element

Set the hidden attribute.

function hide(element) { element.hidden = true; }DOM

Insert given HTML after an element

Parse an HTML string and insert it after the element.

function insertHtmlAfter(html, element) { element.insertAdjacentHTML('afterend', html); }DOM

Insert given HTML before an element

Parse an HTML string and insert it before the element.

function insertHtmlBefore(html, element) { element.insertAdjacentHTML('beforebegin', html); }DOM

Redirect to another page

Navigate to a URL, adding an entry to the history.

function goTo(url) { location.assign(url); }DOM

Replace an element

Swap an element for another in the same position.

function replace(element, replacement) { element.replaceWith(replacement); }DOM

Serialize form data

Turn a form’s fields into an object; repeated names become arrays.

function serialize(form) { const groups = new Map(); for (const [key, value] of new FormData(fo…DOM

Show an element

Clear the hidden attribute and any inline display: none.

function show(element) { element.hidden = false; element.style.removeProperty('display'); }DOM

Strip HTML from a given text

Parse HTML in an inert document and return its text.

function stripHtml(html) { return new DOMParser().parseFromString(html, 'text/html').body.textC…DOM

Toggle an element

Flip the hidden attribute.

function toggle(element) { element.hidden = !element.hidden; }DOM

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.