JavaScriptUse `getElementById()` to find an element by ID, `documentElement` to get the document’s root element, and `body` to access its HTML body. Each can return `null` for a different reason.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptFind an element by its exact ID, access the document’s root element, and read or change its title. Distinguish a missing node from an empty string.
Advanced · Current web platform; runtime support variesJavaScriptLook up an HTML element by its exact ID, handle a missing match, and use `hidden` to change the attribute without removing the element from the DOM.
Advanced · Current web platform; runtime support variesJavaScriptFind an element with `getElementById()`, read the page's current visibility with `document.hidden`, and update the interface on `visibilitychange`.
Advanced · Current web platform; runtime support variesJavaScript`getElementById()` checks whether an element is in the document tree now. `readyState` reports a loading phase, not whether a particular element exists.
Intermediate · Current web platform; runtime support variesJavaScriptRead the document’s current visibility with `document.hidden` and use `visibilitychange` to respond to changes without polling.
Intermediate · Current web platform; runtime support variesJavaScriptUse `DOMContentLoaded` to wait for HTML parsing and `{ once: true }` to limit a listener registration to one call. Check `document.readyState` when your setup code can run late.
Intermediate · HTML Living Standard and DOM Standard; runtime support variesJavaScriptChoose the right listener target for DOM readiness, URL fragment changes, pointer entry, and focus. Use bubbling to handle descendant events from a shared container.
Advanced · Current web platform; runtime support variesJavaScriptUse `DOMContentLoaded` to initialize code once the DOM is built, and `pagehide` to pause work when leaving a document. A bfcache restore is not a fresh load.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptTell when the DOM is ready, when navigation takes you away from a document, and when the browser restores it. Learn what `readyState` and `persisted` actually tell you.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptChoose the event that matches the job: `DOMContentLoaded` for DOM readiness, `pagehide` for leaving a page, and `resize` for viewport size changes.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptDistinguish DOM readiness, page departure, and scrolling. Attach each listener to the right object and account for restoration from the back/forward cache.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptChoose the right event for DOM readiness, delegated pointer and focus handling, or navigation and restoration from the back/forward cache.
Advanced · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptThe read-only `width` property returns the rectangle’s stored width as a number, preserving its sign and any fractional part.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScript`top` tells you where the rectangle’s upper edge is; `height` tells you its signed height. The two properties answer different questions whether you create a rectangle or measure an element.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptRead a rectangle’s width and height, interpret fractional and negative values, and distinguish an element’s measured bounds from its CSS size.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptUse `add()` and `remove()` to update individual tokens, such as an element’s class names, without replacing the whole attribute. Learn when an invalid token makes the call fail.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptChange individual tokens with `add()`, `remove()`, and `toggle()`. Use the `force` argument when a condition should determine whether a token is present.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScript`contains()` checks whether a token is present without changing the list. `toggle()` flips membership or sets it with `force`, then reports whether the token is present.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScriptRead an element's tag name and move among direct children, a parent, and neighboring elements. Learn when you get a live collection and when a property can return `null`.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScript`className` reflects the element’s `class` attribute. `innerHTML` reads serialized markup or parses markup to replace the element’s contents.
Advanced · Current web platform; runtime support variesJavaScriptUse `className` to read or replace the class string, `innerHTML` to read or replace contents as markup, and `toggleAttribute()` to change whether an attribute exists.
Intermediate · ECMAScript 2024 and the current web platform; runtime support variesJavaScript`clientWidth` and `clientHeight` read an element’s inner width and height in whole CSS pixels. They include padding but exclude borders, margins, and space taken up by scrollbars.
Intermediate · Current web platform; runtime support variesJavaScriptMeasure an element’s bounding rectangle in CSS pixels and distinguish viewport-relative coordinates from document coordinates.
Advanced · ECMAScript 2024 and the current web platform; runtime support varies