selected text

R

Rob

Hello all

Is it possible to get which text in a textarea element is selected ?

If so How?

Thanks Rob
 
I

Ivo

Rob said:
Is it possible to get which text in a textarea element is selected ?

The function below returns any selected text (in IE, Netscape and iCab). Or
if no text is selected, null is returned. You have to do additional tests to
see if it is in the textarea. You should call this function onmousedown and
not onclick, because that may unselect the selection.

function isselect(){
if((d=document).getSelection)return d.getSelection();
if(d.selection && d.selection.createRange)return
d.selection.createRange().text;
return false;
}

HTH
Ivo
 
D

Douglas Crockford

Is it possible to get which text in a textarea element is selected ?
The function below returns any selected text (in IE, Netscape and iCab). Or
if no text is selected, null is returned. You have to do additional tests to
see if it is in the textarea. You should call this function onmousedown and
not onclick, because that may unselect the selection.

function isselect(){
if((d=document).getSelection)return d.getSelection();
if(d.selection && d.selection.createRange)return
d.selection.createRange().text;
return false;
}

I would be happier with the function if it looked like this:

function isselect() {
var d = document;
if (d.getSelection) {
return d.getSelection();
} else if (d.selection && d.selection.createRange) {
return d.selection.createRange().text;
}
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top