tags:

views:

35

answers:

1

I'm trying to use the document.getSelection to select text I have entered in an text area for a WYSIWYG editor. It only works if you select text outside the text area though. I don't know if there is a way you can make it select text inside the text area?

Below is the text area for a WYSIWYG text editor:

<iframe id="editor" width="100%" height="400px" contentEditable="true"></iframe>
+2  A: 

You need to get the iframe document's selection:

var ifrDoc = document.getElementById("editor").contentWindow.document,
    sel = ifrDoc.getSelection();

Note that for Internet Explorer you need document.selection.createRange() instead of document.getSelection().

Andy E
bingo thanks. Was so close.
Cory
And for browsers other than IE, you should use the standard DOM Level 2 HTML property `contentDocument` instead of `contentWindow.document`.
bobince
@bobince: I did realize I'd used a non-cross-browser compatible method in my example after writing the first line to be cross-browser compatible. What can I say, other than I felt lazy after writing that first line? :-)
Andy E