[Project_owners] How to get selected text (Solved it)

Jaap A. Haitsma jaap at haitsma.org
Sun Mar 14 16:14:23 EST 2004

Biju G C wrote:
>  --- "Jaap A. Haitsma" <jaap at haitsma.org> wrote: > Hi,
>>I'm trying to obtain the text that is selected. If text is selected in a 
>>web page (in Firefox) or in a message (in Thunderbird) I can obtain the 
>>text very easily by calling gContextMenu.searchSelected(). However if 
>>the selected text is inside a <textarea> or <input> tag (Firefox) or in 
>>the message composer (Thunderbird) I don't manage to get the selected text.
>>I've been looking in the mozilla cross reference 
>>http://lxr.mozilla.org/mozilla/ for quite a while to try to figure out 
>>how the command copy works (because that is available at the points I 
>>want to get the selection), but sofar I'm still not able to get it 
>>working. :(
Somebody at mozillazine forums pointed me at the text/plain extension
which does what I wanted. See http://mozilla.durys.net/textplain/

The relevant part of the code to get the selection is this part
var node = document.popupNode;

var selection = "";

var nodeLocalName = node.localName.toUpperCase();

if ((nodeLocalName == "TEXTAREA") ||
     (nodeLocalName == "INPUT" && node.type == "text")) {

     selection = node.value.substring(node.selectionStart, 


else {
     var focusedWindow = document.commandDispatcher.focusedWindow;

     selection = 


selection = selection.toString();

The first part gets the selection from a <textarea> or <input> tag
The second part picks up the text if you selected something in a web 
page or in the message pane and even the message composer of 
thunderbird. So this solves everything.

The code in text/plain also picks up values from <select> statements but 
I do not see that to be very useful. As a reference I installed the 
relevant js file of text/plain

-------------- next part --------------
A non-text attachment was scrubbed...
Name: TextPlain.js
Type: application/x-javascript
Size: 7478 bytes
Desc: not available
Url : http://mozdev.org/pipermail/project_owners/attachments/20040314/61ec037e/TextPlain.js

More information about the Project_owners mailing list