I have this problem with Chrome and Safari (works in Mozilla and IE + Edge).
I want to store the caret position and selection using range=sel.getRangeAt(0);
However, I get the following error in Chrome:
Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index.
How can I fix this in another way than the following: here.
This is my fiddle
The reason why I can't use this method is because I display a hidden
div
, and insert text into an <input type="text">
field, I then insert that into a contenteditable
div
. Selecting this input field makes me save a new selection, and messes up the insert.
Here are some images to display the problem:
Part 1: The contenteditable
div fields
Part 2: The hidden div that is displayed, and typing, changing the carter position
Part 3: On insert, console error in Chrome and Safari
Any suggestions?