Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of window.getSelection() changes during callback

I am trying to store the value of window.getSelection(), but this value seems to change if the user selects another selection.

In my code below, I have stored the initial user's selection. I then create my form (which takes a bit of time), and when the form is finished being created I fill in the form with the user's selection. Now during the time when the form is being created, the user may select some other text, and now the object stored in my selection variable has changed. How would I prevent the value of selection from being changed?

var selection = window.getSelection();

createForm(function() {
    fillInForm(selection);
});

EDIT - I need to keep the DOM structure of the selection as the form references the position of the selected text.

like image 762
Jon Avatar asked Dec 04 '25 13:12

Jon


1 Answers

The getSelection function returns a Selection object. There can only be one selection object per document, so the range associated with the selection must change if the user makes a new selection.

The user agent should allow the user to change the active document's selection. If the user makes any modification to a selection, the user agent must create a new range with suitable start and end and associate the selection with this new range (not modify the existing range).

[...]

If the user changes the selection or a script calls addRange(), the selection must be associated with a new range object, as required elsewhere in this specification.

http://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#selections

like image 112
Dagg Nabbit Avatar answered Dec 07 '25 03:12

Dagg Nabbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!