I was wondering wether it was possible to know exactly where in the dom the text that is selected is?
I am currently using the following function to know what text is being selected, but I want to be able to return its position as well
Selector = {};
Selector.getSelected = function(){
var t = '';
if(window.getSelection){
t = window.getSelection();
}else if(document.getSelection){
t = document.getSelection();
}else if(document.selection){
t = document.selection.createRange().text;
}
return t;
}
and I can also find what the parent element is with the following:
function getSelectionStart(){
var node = document.getSelection().anchorNode;
var startNode = (node.nodeName == "#text" ? node.parentNode : node);
return startNode;
}
but is there a way for me to know with js what the character position it has depending on where I click?
I ended up dynamically addressing each character as a seperate span tag and it’s index.
This is what is currently doing what I want.
It isn’t elegant, clean or light but it gets the job done.