I'm trying to store a selection of a contentEditable element and restore it later.
I want to observe the paste
event and store the HTML as it was before, clear the html and then manually insert the pasted text with some changes at the selected position.
Take a look at this example: jsfiddle.net/gEhjZ
When you select a part of the text, hit store
, remove the selection again and hit restore
, it's working as expected.
But when you first hit store
, then replace the HTML with the exact same HTML by hitting overwrite html
and then try to restore
, nothing happens.
I thought that using .cloneRange()
would make a difference, but it won't. Even a deep copy of the object ($.extend(true, {}, oldRange)
) won't do the trick. As soon as I overwrite the HTML, the selection object sel
is being changed too. It makes sense for me that changing the selection context will wipe the range, but I'm trying to restore it for the exact same HTML.
I know I could use rangy, but I really don't want to use a huge library just for this small feature. What am I missing? Any help would be much appreciated!
Note: only Firefox/Chrome, so no crossbrowser-hacks needed.
Update:
@Tim Down's answer works when using a div, but I'm actually using an iframe. When I made that example, I thought it wouldn't make any difference.
Now when I try to restore the iframe's body, i get the following error: TypeError: Value does not implement interface Node.
in the following line preSelectionRange.selectNodeContents(containerEl);
. I didn't get much from googling. I tried to wrap the contents of the body and restore the wrap's html, but I get the same error.
jsfiddle isn't working in this case because it is using iframes to display the results itself, so I put an example here: snipt.org/AJad3
And the same without the wrap: snipt.org/AJaf0
Update 2:
I figured that I have to use editable.get(0)
, of course. But now the start
and end
of the iframe's selection is 0. see snipt.org/AJah2
See Question&Answers more detail:
os