Is it possible to set a Range object as a "reverse selection"?

I create a Range object and then add this Range to highlight

window.getSelection ().addRange(myRange); 

How to set the direction of choice? I am referring to a direction that can be checked using the properties anchorNode , anchorOffset , focusNode and focusOffset .

+6
source share
1 answer

You can do this in browsers that support extend() ( MDN ) for Selection objects. Support for Mozilla, WebKit and Opera; IE does not support and does not include version 11. extend() was added to the HTML Editing API specification , so it can appear in IE.

Here's an example function:

 function selectRangeBackwards(range) { if (typeof window.getSelection != "undefined") { var sel = window.getSelection(); if (typeof sel.extend != "undefined") { var endRange = range.cloneRange(); endRange.collapse(false); sel.removeAllRanges(); sel.addRange(endRange); sel.extend(range.startContainer, range.startOffset); } } } 
+5
source

Source: https://habr.com/ru/post/898869/


All Articles