I have this code with which I put selected text inside a span that gives it a background-color with CSS.
When I try to 'mark' text from two different divs, or text which is inside any two tags, I get an error:
Uncaught Error: BAD_BOUNDARYPOINTS_ERR: DOM Range Exception 1
This is my code:
function highlightSelection() {
var selection;
//Get the selected stuff
if(window.getSelection)
selection = window.getSelection();
else if(typeof document.selection!="undefined")
selection = document.selection;
//Get a the selected content, in a range object
var range = selection.getRangeAt(0);
//If the range spans some text, and inside a tag, set its css class.
if(range && !selection.isCollapsed)
{
var span = document.createElement('span');
span.className = 'highlight-green';
range.surroundContents(span);
}
}
The highlightSelection() is called with onmouseup
event.
Thanks ahead!