Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Knowledge base — JavaScript

Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.

Entries: 1003

JavaScript

Finding an element by ID and accessing the document root and body

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.

Advanced · ECMAScript 2024 and the current web platform; runtime support varies
JavaScript

DOM document: ID lookup, root element, and title

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.

Advanced · Current web platform; runtime support varies
JavaScript

Find an element by ID and change its `hidden` state

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.

Advanced · Current web platform; runtime support varies
JavaScript

DOMContentLoaded and the once listener option

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.

Intermediate · HTML Living Standard and DOM Standard; runtime support varies
JavaScript

DOMContentLoaded and pagehide events

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.

Advanced · ECMAScript 2024 and the current web platform; runtime support varies
JavaScript

DOMContentLoaded, pagehide, and resize events

Choose 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 varies
JavaScript

DOMContentLoaded, pagehide, and scroll events

Distinguish 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 varies
JavaScript

DOMRectReadOnly.width — rectangle width

The 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 varies
JavaScript

DOMRect: top edge (`top`) and height (`height`)

`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 varies
JavaScript

DOMRect: width and height properties

Read 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 varies
JavaScript

DOMTokenList: Adding and removing tokens

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.

Intermediate · ECMAScript 2024 and the current web platform; runtime support varies
JavaScript

DOMTokenList: Add, remove, and toggle tokens

Change 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 varies
JavaScript

DOMTokenList: checking and toggling tokens

`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 varies
JavaScript

DOM element traversal: children, tag name, parent, and siblings

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`.

Intermediate · ECMAScript 2024 and the current web platform; runtime support varies
JavaScript

Element: classes, contents, and attribute presence

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.

Intermediate · ECMAScript 2024 and the current web platform; runtime support varies
JavaScript

Element inner dimensions: clientWidth and clientHeight

`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 varies