IE selection range

I searched for days, but could not find one solid solution to get the start / end of the selection in Internet Explorer, I tried this and this solution.

Both of these are related to the use of document.selection.createRange (), the code below will work fine, but if you go to a new line (press enter one or more times), and then try to get the start / end of the selection from you to end as you press enter. From what I see, createRange () will not select the last empty line breaks, I don't want the code to get too dirty, so is there any other solution for this?

function selecetion_range()
{
 var range = document.selection.createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(textarea[0]);
    stored_range.setEndPoint('EndToEnd', range );

    return {
          start: stored_range.text.replace(/\r/g,'').length - range.text.replace(/\r/g,'').length,
          end: stored_range.text.replace(/\r/g,'').length
 }
}

change

To clarify the problem, let's say my cursor is at the end of this quote:

"some random text

<here> "

, :

" <here> "

+3
1

, , .

+2

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


All Articles