18

I recently noticed the following message in chrome's console log, while using aloha editor:

aloha.js:14579 - The behavior that Selection.addRange() merges existing Range and the specified Range is deprecated and will be removed in M58, around April 2017. See https://www.chromestatus.com/features/6680566019653632 for more details.

While trying to find a replacement, i couldn't find anything besides that they are going to remove it, so i would like to know what are the alternatives for Selection.addRange() to get rid of this message.

Community
  • 1
  • 1
Inc33
  • 1,278
  • 1
  • 15
  • 22

1 Answers1

33

The trick is to use removeAllRanges() on your selection before adding your new range using addRange(range). Here is an example when using it to select all content of elem:

selection = window.getSelection();    // Save the selection.
range = document.createRange();
range.selectNodeContents(elem);
selection.removeAllRanges();          // Remove all ranges from the selection.
selection.addRange(range);            // Add the new range.
Raymundus
  • 1,637
  • 1
  • 18
  • 31