Programmatically focusing contenteditable div selects entire content in Chrome and Safari
OS: macOS 13-14 · Device: Desktop or Laptop Any · Browser: Chrome or Safari 120+ · Keyboard: US QWERTY
Open case →Scenario
In Chrome and Safari, calling focus() on a contenteditable div can select the entire content instead of placing the cursor at the beginning, as observed in Firefox and IE.
In Chrome and Safari, calling focus() on a contenteditable div can select the entire content instead of placing the cursor at the beginning, as observed in Firefox and IE.
focus() selects all contentAfter focus(), manually set cursor position using Selection API:
function focusEditor() {
editor.focus();
const selection = window.getSelection();
const range = document.createRange();
const firstNode = getFirstTextNode(editor);
if (firstNode) {
range.setStart(firstNode, 0);
range.setEnd(firstNode, 0);
} else {
range.selectNodeContents(editor);
range.collapse(true);
}
selection.removeAllRanges();
selection.addRange(range);
}
function getFirstTextNode(node) {
if (node.nodeType === Node.TEXT_NODE) {
return node;
}
for (let child of node.childNodes) {
const textNode = getFirstTextNode(child);
if (textNode) {
return textNode;
}
}
return null;
}
Create a range at the beginning and set selection:
editor.addEventListener('focus', () => {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
});
Handle focus events to reset selection:
editor.addEventListener('focus', (e) => {
// Reset selection to beginning
setTimeout(() => {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(editor);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}, 0);
});
Visual view of how this scenario connects to its concrete cases and environments. Nodes can be dragged and clicked.
Each row is a concrete case for this scenario, with a dedicated document and playground.
| Case | OS | Device | Browser | Keyboard | Status |
|---|---|---|---|---|---|
| ce-0304-chrome-safari-focus-selects-all-en | macOS 13-14 | Desktop or Laptop Any | Chrome or Safari 120+ | US QWERTY | draft |
Open a case to see the detailed description and its dedicated playground.
OS: macOS 13-14 · Device: Desktop or Laptop Any · Browser: Chrome or Safari 120+ · Keyboard: US QWERTY
Open case →Other scenarios that share similar tags or category.
When a contenteditable element contains another contenteditable element, focus behavior becomes unpredictable. Clicking on the nested element may not properly focus it, and selection ranges may span across both elements incorrectly.
The autofocus attribute, which automatically focuses form inputs on page load, does not work on contenteditable elements. There is no built-in way to automatically focus a contenteditable region when a page loads.
When a contenteditable region contains interactive elements (buttons, links, etc.), clicking on these elements causes the contenteditable to lose focus. This interrupts the editing flow and may cause the caret to disappear.
When a contenteditable element enters or exits fullscreen mode using the Fullscreen API, focus and selection may be lost. The caret position may reset, and editing may be disrupted.
When a page with a contenteditable element becomes hidden (tab switch, minimize), the Page Visibility API may affect editing state. Focus may be lost, and composition may be interrupted.
Have questions, suggestions, or want to share your experience? Join the discussion below.