Check if a given DOMRect is contained within another DOMRect
True when the target lies inside the container or on its edges.
function isContained({ target, container }) { return target.x >= container.x && target.x + targ…DOMCollection · 30 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.
True when the target lies inside the container or on its edges.
function isContained({ target, container }) { return target.x >= container.x && target.x + targ…DOMTwo 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.…DOMCompare x, y, width and height exactly.
function sameRect({ a, b }) { return ['x', 'y', 'width', 'height'].every(key => a[key] === b[ke…DOMReturn [x, y] of the rect’s center.
function getCenter({ x, y, width, height }) { return [x + width / 2, y + height / 2]; }DOMPixels left to scroll before the end of the page.
function distanceToBottom() { return document.documentElement.scrollHeight - window.innerHeight…DOMTrue when either axis is styled to scroll: overflow auto or scroll.
function hasCssOverflow(element) { const { overflowX, overflowY } = getComputedStyle(element); …DOMTrue when the content is larger than the element’s visible box.
function hasOverflow(element) { return element.scrollHeight > element.clientHeight || element.s…DOMTrue when child is inside parent, at any depth.
function isDescendant(child, parent) { return child !== parent && parent.contains(child); }DOMCompare the element with document.activeElement.
function hasFocus(element) { return element === document.activeElement; }DOMTrue when the browser exposes touch events or reports touch points.
function touchSupported() { return 'ontouchstart' in window || navigator.maxTouchPoints > 0; }DOMTrue when the viewport has reached the end of the page.
function isAtBottom(tolerance = 1) { const { scrollHeight, clientHeight } = document.documentEl…DOMLook for document.documentMode, which only Internet Explorer defines.
function isInternetExplorer() { return 'documentMode' in document; }DOMGuess whether the device is a Mac, iPhone or iPad from the platform string.
function isApplePlatform() { return /Mac|iPhone|iPad|iPod/.test(navigator.userAgentData?.platfo…DOMReturn the other element children of the element’s parent.
function siblings(element) { return element.parentElement ? [...element.parentElement.children]…DOMAdd the scroll offsets to the element’s viewport position.
function getPosition(element) { const rect = element.getBoundingClientRect(); return { left: re…DOMRead the text of the current document selection.
function getSelectedText() { return window.getSelection()?.toString() ?? ''; }DOMMove one step back in the session history.
function goBack() { history.back(); }DOMSet the hidden attribute.
function hide(element) { element.hidden = true; }DOMPlace an element right after a reference element.
function insertAfter(element, reference) { reference.after(element); }DOMPlace an element right before a reference element.
function insertBefore(element, reference) { reference.before(element); }DOMParse an HTML string and insert it after the element.
function insertHtmlAfter(html, element) { element.insertAdjacentHTML('afterend', html); }DOMParse an HTML string and insert it before the element.
function insertHtmlBefore(html, element) { element.insertAdjacentHTML('beforebegin', html); }DOMNavigate to a URL, adding an entry to the history.
function goTo(url) { location.assign(url); }DOMLoad the current document again.
function reload() { location.reload(); }DOMSwap an element for another in the same position.
function replace(element, replacement) { element.replaceWith(replacement); }DOMScroll the window back to the top.
function goToTop() { window.scrollTo(0, 0); }DOMTurn 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…DOMClear the hidden attribute and any inline display: none.
function show(element) { element.hidden = false; element.style.removeProperty('display'); }DOMParse HTML in an inert document and return its text.
function stripHtml(html) { return new DOMParser().parseFromString(html, 'text/html').body.textC…DOMFlip the hidden attribute.
function toggle(element) { element.hidden = !element.hidden; }DOMTry a function in the local code lab, then read its limits before using it in an application.