`document.getElementById()` versus `document.body`
Use `getElementById()` to find an element by its ID, and `document.body` to access the document’s body element whether or not it has an ID.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Use `getElementById()` to find an element by its ID, and `document.body` to access the document’s body element whether or not it has an ID.
`getElementById()` finds an element by its exact ID, while `documentElement` gives you the document's root element. Both can yield `null`, but for different reasons.
Use `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.
Find 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.
Look 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.
Find an element with `getElementById()`, read the page's current visibility with `document.hidden`, and update the interface on `visibilitychange`.
`getElementById()` checks whether an element is in the document tree now. `readyState` reports a loading phase, not whether a particular element exists.
Read the document’s current visibility with `document.hidden` and use `visibilitychange` to respond to changes without polling.
Use `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.
Choose the right listener target for DOM readiness, URL fragment changes, pointer entry, and focus. Use bubbling to handle descendant events from a shared container.
Use `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.
Tell 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.
Choose the event that matches the job: `DOMContentLoaded` for DOM readiness, `pagehide` for leaving a page, and `resize` for viewport size changes.
Distinguish DOM readiness, page departure, and scrolling. Attach each listener to the right object and account for restoration from the back/forward cache.
Choose the right event for DOM readiness, delegated pointer and focus handling, or navigation and restoration from the back/forward cache.
The read-only `width` property returns the rectangle’s stored width as a number, preserving its sign and any fractional part.
`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.
Read a rectangle’s width and height, interpret fractional and negative values, and distinguish an element’s measured bounds from its CSS size.
Use `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.
Change individual tokens with `add()`, `remove()`, and `toggle()`. Use the `force` argument when a condition should determine whether a token is present.
`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.
Read 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`.
`className` reflects the element’s `class` attribute. `innerHTML` reads serialized markup or parses markup to replace the element’s contents.
Use `className` to read or replace the class string, `innerHTML` to read or replace contents as markup, and `toggleAttribute()` to change whether an attribute exists.