2

Is there any alternative to document.getSelection().addRange()?

In a content editable frame containing divs, it gets slower as we go down the divs.

Azeem
  • 11,148
  • 4
  • 27
  • 40

1 Answers1

1

Are you running removeAllRanges first? The MDN article and this SO answer make it seem like that call is required / recommended.

An example script from the MDN article:

/* Select all STRONG elements in an HTML document */

var strongs = document.getElementsByTagName("strong");
var s = window.getSelection();

if(s.rangeCount > 0) s.removeAllRanges();

for(var i = 0; i < strongs.length; i++) {
  var range = document.createRange();
  range.selectNode(strongs[i]);
  s.addRange(range);
}
flygaio
  • 121
  • 5
  • yes. removeAllRanges is called first. Also taking first Range object, calling deleteContents and then doing setStart and setEnd for that range doesnot help – user8173591 Jul 28 '17 at 08:22