jQuery get html selection

SweetSuzy777

New Member
I've been searching online for a while trying to find a solution to this. What I want to do is convert text selection start and end points to their equivalent html selection values.Example:\[code\]HTML: test text **some <span style="color:red">te**st t</span>extTEXT: test text **some te**st text\[/code\]So say the user selects the parts surrounded in ** above. I have no problem getting the position of the text selection: start: 10, end: 17. But what I want to get is the position of the selection including the html elements: start: 10, end: 41Can anyone suggest a solution for this?EDIT: as requested I've attached my existing code which gets the start and end points of the selected text. What I want to get are the start and end points of the selected HTML.\[code\] var start = 0, end = 0; var sel, range, priorRange; if (typeof window.getSelection != "undefined") { range = window.getSelection().getRangeAt(0); priorRange = range.cloneRange(); priorRange.selectNodeContents(el[ 0 ]); priorRange.setEnd(range.startContainer, range.startOffset); start = priorRange.toString().length; end = start + range.toString().length; } else if (typeof document.selection != "undefined" && (sel = document.selection).type != "Control") { range = sel.createRange(); priorRange = document.body.createTextRange(); priorRange.moveToElementText(el[ 0 ]); priorRange.setEndPoint("EndToStart", range); start = priorRange.text.length; end = start + range.text.length; }\[/code\]
 
Back
Top