The following code finds out the beginning of the user's selection, if the user e.g. selects the text "text", this is "18".
<p>I am some example text, you don't have to read it :P</p>
<script>
function findsel(){
console.log(window.getSelection().baseOffset);
// Returns 18
}
</script>
If the paragraph is interrupted ...
<p id="mytext">I am some <em>example</em> text, you don't have to read it :P</p>
<script>
function findsel(){
console.log(window.getSelection().baseOffset);
// Returns 1
}
</script>
... and the user selects "text" again, the selection's beginning will be "1", because it starts counting at the </em>
tag.
Is there a way to refer the getSelection method to a certain element (#mytext
in this case) so it starts counting a the beginning of this specified element and not at the last in-between node?