samedi 19 avril 2014

Get HTML content of content editable div before caret


Vote count:

0




I am trying to to get the get the caret offest inside a content editable div that includes the offset of the HTML.


For example:



<div contenteditable=true>
hello &nbsp;|(caret is here)
</div>


In the above example the caret offset should be 5 ("hello") + 1(" ") + 6("&nbsp;") = 12


Instead I am getting offset=7 as &nbsp; is being converted to a space and counted as 1.


See my code below:



var range1 = window.getSelection().getRangeAt(0),
range2 = range1.cloneRange();
range2.selectNodeContents(target);
range2.setEnd(range1.endContainer, range1.endOffset);
return range2.toString().length;




Any help would be appreciated!



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire