views:

1432

answers:

4

I'm trying to find a way with javascript to highlight the text the user selects when they click some odd highlight button (as in <span style="background-color:yellow">highlighted text</span>). It only has to work with either WebKit or Firefox, but it seems to be well nigh impossible because it has to work in the following cases:

<p>this is text</p>
<p>I eat food</p>

When the user selects from "is text" through "I eat" in the browser (can't just put a span there).

and this case:

<span><span>this is text</span>middle text<span>this is text</span></span>

When the user selects from "is text" to "this is" in the browser (even though you can wrap your highlight spans around each element in the selection, I'd like to see you try to get that middle text highlighted).

This problem doesn't seem to be solved anywhere, frankly I doubt it's possible.

It would be possible if you could get the Range that you get from the selection as a string complete with html which could be parsed and then replaced, but as far as I can tell you can't get the raw html of a Range.. pity.

+1  A: 
<html>
<head>
<script type="text/javascript">
function load(){
  window.document.designMode = "On";
  //run this in a button, will highlight selected text
  window.document.execCommand("hiliteColor", false, "#000");
}
</script>
</head>
<body contentEditable="true" onload="load()">
  this is text
</body>
</html>
foobar
This is easily the best idea. Good answer, but it would really be an idea to turn `designMode` off again afterwards.
Tim Down
A: 

A possibility that works (although really, really, really slow) is to insert around every letter some kind of id. <div>A<span>#23125#</div></span> Of course you'd have to hide it, maybe by setting it's overflow to none and it's max-width to 1em. (That would of course give interesting results for C&P.) Then it would be possible to find the selection and highlight it.

Maybe you could also track the mouse where it's mousedown and mouseup events are and establish the selected region. Then you could create a on top of the found rectangle.

But really, I can't see a useful method beside the one foobar proposed for the Internet Explorer.

Georg
+2  A: 

Well, you can do it using DOM manipulation. This works in Firefox:

var selection = window.getSelection();
var range = selection.getRangeAt(0);
var newNode = document.createElement("span");
newNode.setAttribute("style", "background-color: pink;");
range.surroundContents(newNode);

Seems to work in the current version of Safari as well. See https://developer.mozilla.org/en/DOM/range.surroundContents and http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html

Cugel
A: 

Hi, I am trying to display the selected text in an alert box in Mozilla Firefox. This is my following code:

var selection = window.getSelection(); alert(selection.toString());

However, nothing is being displayed when the user highlights the text on a web page and clicks the button (to execute the above function).

any reason why?

In addition I tried the code suggested by Cugel but doesn't work either!

Any input would be greatly appreciated.

Kind regards

Chris