rangy icon indicating copy to clipboard operation
rangy copied to clipboard

How to get character position of selected text across multiple html element

Open quantrung9 opened this issue 9 years ago • 0 comments

Hi,

I try to write a function get start_character, end_character position in an article (an article has multiple p tags, div tags elements) like this:

<p>Let’s face it: you’re not as fit as you’d like to be. But that doesn’t mean you should just give up.</p>
<p>The first step is learning what can be improved. Do you eat too many processed foods, drink too much alcohol or caffeinated beverages, or take paracetamol or other medications unnecessarily? These are the kinds of bad habits that lead to chronic inflammation and damage your body.</p>

For example, if I select "should just give up. The first step is learning what can be improved", the start_character should be 80 and end_character should be 147.

I really need start and end position to render on the mobile app, not only on the web.

I try to use the window.getSelection() and focusOffset property to get it (https://jsfiddle.net/quan/wLbppn1b/) but the window.getSelection() doesn't support selection across html element.

I googled and found your library, and I checked your demo, but I don't know how to do this with your library. Hope you can help me.

Thanks,

quantrung9 avatar Aug 11 '16 19:08 quantrung9