I want to find the range of the selected text with respect to the parent element. So in this <p> element, the anchorOffset & focusOffset for "fox" is [16,19]
<p>The quick brown fox jumps over the lazy dog.</p> But if we add a <b> tag before "fox" then the values change to [1,4].
<p>The quick <b>brown</b> fox jumps over the lazy dog.</p> My best guess is, the index count starts from the end of </b> tag. But I want it to still show the original value, irrespective of the HTML within <p>. For this, I tried creating a Range, but still couldn't figure it out. Following is the code, as a function.
function rangefun() { var range = document.createRange(); var referenceNode = document.getElementsByTagName('P').item(0); range.selectNode(referenceNode); var selection = window.getSelection(); var start = selection.anchorOffset; var end = selection.focusOffset; console.log("start: " + start); }