Alex Rivera | Logout

window.getSelection() gives me the selected text, but I want the HTML

Asked 2010-01-26T13:29:09.940
39

I'm extending a WYSIWYG HTML editor (for Firefox), I want to add tags around a selection. I can't find a function to accomplish this in the Mozilla Midas specification.

There is a command to replace the selection with HTML.
So if I could read the selection contents, I could add my tags to this string.

window.getSelection() almost works, but it gives me nsISelection which converts to a plain-text string.

PS: document.getSelection() returns the plain-text string not even a nsISelection.

Edit
Report

1 Answer

9

window.getSelection() will return an object. You can use the returned selection object as a string by calling the objects .toString() method.

var selObj = window.getSelection();
var selectedText = selObj.toString(); 

https://developer.mozilla.org/en/DOM/window.getSelection

answered 2011-11-20T03:32:07.947

Your Answer