Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript document.getSelection

Tags:

javascript

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>
like image 619
Cory Avatar asked Feb 12 '26 23:02

Cory


1 Answers

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().

like image 119
Andy E Avatar answered Feb 15 '26 11:02

Andy E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!