Set selection by range in Javascript -


i want select text sending location/anchoroffset , length/offset using javascript here code

  var node = document.getelementbyid("content");    var range = document.createrange();    range.setstart(node, 0);    range.setend(node, 4); // here 0 , 4 location , length selection        // if string "this test string" in case must select "this"     var selection = window.getselection();    selection.removeallranges();     selection.addrange(range); 

but issue range.setstart , range.setend not working expect

the setstart() , setend() methods of dom range specified, find answer reading the spec or mdn.

to summarise, if want specify range boundary in terms of character offsets, need deal text node rather element. if element contains single text node, changing first line of code following work:

var node = document.getelementbyid("content").firstchild; 

Comments