CKTIS / JavaScript
ID lookup versus focus: getElementById() and activeElement
`document.getElementById(id)` finds an element by its ID, while `document.activeElement` reports where the document sees keyboard focus right now.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
document.getElementById(id) // call: Element or null
document.activeElement // read-only property: Element or nullDocument.getElementById/activeElement pairs two different operations. document.getElementById(id) is a method that looks up an id in that document's tree. Pass the ID itself, without the CSS selector prefix #. Matching is case-sensitive. The method returns an Element, or null if nothing matches. IDs should be unique within a tree; if there are duplicates, the method returns the first match in tree order.
The lookup excludes detached nodes and does not enter shadow trees or the documents inside iframe elements. DocumentFragment, and therefore ShadowRoot, has its own getElementById() method; an ordinary Element does not. Check for null before using the result, or use ?. if a missing element is acceptable.
document.activeElement is a read-only property, not a method. It returns the Element that represents current keyboard focus from this document's point of view. When focus is inside a shadow tree or an embedded document, the outer document sees the shadow host or the iframe, respectively. To inspect further, read activeElement on an accessible ShadowRoot or on the same-origin frame's document. When focus resolves to the document itself, the property falls back to its body, then to documentElement if it exists. It returns null when no element or fallback applies in the queried tree.
Focus is not text selection, and looking up an element does not focus it. If target is not null, document.activeElement === target tests node identity. Read the property again after focus changes: a saved value does not update on its own. These are web platform APIs, not ECMAScript 2024 features; a runtime without a browser DOM need not provide document.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ID lookup and focus</title>
</head>
<body>
<script>
{
const other = document.createElement('input');
const target = document.createElement('input');
target.id = 'cktis-target';
document.body.append(other, target);
try {
const found = document.getElementById('cktis-target');
other.focus();
console.log(found === target);
console.log(found === document.activeElement);
found?.focus();
console.log(found === document.activeElement);
console.log(document.getElementById('CKTIS-target') === null);
} finally {
other.remove();
target.remove();
}
}
</script>
</body>
</html>Result
true false true true
Save the example as an HTML file, open it in a foreground browser tab, and then check the browser console. If the browser allows programmatic focus, the first three lines show that the target is found, is not initially focused, and then gains focus. The last line confirms that changing the ID's case prevents a match.
Related lessons
- JavaScript · Lesson 18 · Advanced · Version: 4