I am attempting to put together a java script that, upon a user's selection of some text inside a specific div id, will return that selection's start and end indices ignoring internal html tags.
Consider the following example:
<p>Some text that will be ignored</p>
<div id="X">
<p>Here is some text that should be considered</p>
<p>Here is a bit more <span>tex</span>t in a separate paragraph but the same div id</p>
</div>
Now, If I highlight be considered. Here is
and use
var currentRange = window.getSelection().getRangeAt(0);
console.log('startContainer\t'+currentRange.startContainer);
console.log('startOffset\t'+currentRange.startOffset);
console.log('endContainer\t'+currentRange.endContainer);
console.log('endOffset\t'+currentRange.endOffset);
console.log('textLength\t'+currentRange.toString().length);
console.log('text\t'+currentRange.toString())
I get startOffset:29 and endOffset: 4.
My Question:
Is it possible to keep track of which <p>
tag I am inside of while using the Range() object? I will eventually have more html elements in this div tag, but want to be able to get the indices range of the selection as though it were all one string, ignoring all html elements inside.
Thanks for all the help!