getSelection & surroundContents across multiple tags

Posted by lethalbody on Stack Overflow See other posts from Stack Overflow or by lethalbody
Published on 2010-04-06T11:09:52Z Indexed on 2010/04/06 11:13 UTC
Read the original article Hit count: 446

Filed under:
|

Hi,

I've got a script that changes the background colour of text that has been selected. However i'm encountering an issue when the text is selected across multiple elements/tags.

The code that i've got is:

var text = window.getSelection().getRangeAt(0);
var colour = document.createElement("hlight");
colour.style.backgroundColor = "Yellow";
text.surroundContents(colour);

And the error being output is:

Error: The boundary-points of a range does not meet specific requirements. =
NS_ERROR_DOM_RANGE_BAD_BOUNDARYPOINTS_ERR
Line: 7

I believe this is to do with the getRange() function though i'm not too sure how to proceed since I am a beginner at javascript.

Is there any other way I can replicate what I am trying to achieve?

Many thanks.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about getselection