selection.addRange works correctly in Chrome
OS: Windows 10/11 · Device: Desktop Any · Browser: Chrome 120+ · Keyboard: English (QWERTY)
Open case →Scenario
When setting cursor position using `selection.addRange()` in a contenteditable element, it works correctly in Chrome and Firefox but fails in Safari. The selection "pops out" of intended marker element and moves to the next sibling's text node instead of staying within the marker.
When using window.getSelection().addRange() to set cursor position in a contenteditable element, Safari exhibits incorrect behavior that breaks the intended selection placement.
This issue occurs when:
<span> markers)selection.addRange()Safari’s WebKit selection API has a known issue with nested elements and addRange():
This is particularly problematic when:
<span> tags with specific IDs)selection.addRange() instead of selection.collapse()Use selection.collapse() with node and offset:
const range = document.createRange();
range.setStart(textNode, offset);
range.collapse(true); // true = collapse to end
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
Use setTimeout with selection manipulation:
setTimeout(() => {
const range = document.createRange();
range.setStart(element, 0);
range.collapse(true);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}, 10);
Force focus on the element first:
targetElement.focus();
setTimeout(() => {
// Now try setting selection
const range = document.createRange();
range.setStart(targetElement, 0);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}, 0);
Avoid nested elements:
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-0255-selection-addrange-chrome-en | Windows 10/11 | Desktop Any | Chrome 120+ | English (QWERTY) | confirmed |
| ce-0256-selection-addrange-edge-en | Windows 10/11 | Desktop Any | Edge (Chromium-based) 120+ | English (QWERTY) | confirmed |
| ce-0545-selection-addrange-firefox-en | Windows 10/11 | Desktop Any | Firefox 120+ | English (QWERTY) | confirmed |
This matrix shows which browser and OS combinations have documented cases for this scenario. Click on a cell to view the specific case.
| Browser | Windows |
|---|---|
| Chrome | |
| Edge (Chromium-based) | |
| Firefox |
Open a case to see the detailed description and its dedicated playground.
OS: Windows 10/11 · Device: Desktop Any · Browser: Chrome 120+ · Keyboard: English (QWERTY)
Open case →OS: Windows 10/11 · Device: Desktop Any · Browser: Edge (Chromium-based) 120+ · Keyboard: English (QWERTY)
Open case →OS: Windows 10/11 · Device: Desktop Any · Browser: Firefox 120+ · Keyboard: English (QWERTY)
Open case →Other scenarios that share similar tags or category.
When editing content inside an element with `position:relative`, the text caret (cursor) is completely invisible. Text can be typed and appears in the editor, but there's no visual feedback of where the insertion point is located.
When contenteditable='false' elements are placed inside a contenteditable container, the cursor may disappear or become invisible in certain browsers, making it difficult for users to determine the text insertion point.
After copying selected text in a contenteditable region using Cmd+C, the selection is lost in Safari. The user must re-select the text to perform additional operations.
When a contenteditable region loses focus, window.getSelection() may return null in Safari, even if there was a valid selection before the focus loss. This makes it difficult to preserve or work with selections.
After programmatically manipulating the DOM in a contenteditable element, restoring the text selection (cursor position) is unreliable across browsers. The selection may be lost, moved to an incorrect position, or become invalid.
Have questions, suggestions, or want to share your experience? Join the discussion below.