PDA

View Full Version : Get Selected Text In Text Area In Internet Explorer



michanagel
11 Mar 2012, 2:19 AM
Hi,

I have a problem here that I can't seem to solve... I took over a 3.x project and there is a custom extension for the HTML Editor for text areas that provided additional functionality for the URL (hyperlink) button...

In order to execute the additional functionality the extension needs to get the selected text in the text area... It works fine for Firefox, Safari and Chrome but it does not work for IE.

Here's the current code that gets the text selection:

win = this.getWin();
var sel = win.getSelection ? win.getSelection() : win.document.selection;

IE does not return the text... I have tried these suggested solutions for IE:

http://www.sencha.com/forum/showthread.php?78424-Ext.Element-should-standardize-Selection-range

still no go...

Can anybody help ?

Thanks in advance for your help !!!

- M

mitchellsimoens
11 Mar 2012, 6:51 AM
This should work on IE:


text = (document.all) ? document.selection.createRange().text : document.getSelection();

michanagel
11 Mar 2012, 2:19 PM
@mitchellsimoens (http://www.sencha.com/forum/member.php?22216-mitchellsimoens) : thanks for your reply !

I tried your solution and it does not work for any browser in the Ext JS 3.x project.

In a standalone simple test text area (plain HTML with an attached JavaScript function) your solution returns the selected text in IE but not in FF, Safari and Chrome...

Btw, I noticed with my solution up top that on IE the selected text gets un-selected when it processes it and tries to get the text selection... that does not happen in any of the other browsers...

Could that be the reason why it is returning an empty string ?

Thanks.

- M