Can JS detect SELECTED text in a textArea

M

Martin Honnen

STB wrote:

Can you pick out just the text that a user has selected in a TextArea using
JS?

It depends on whether the browser offers an API, some like Netscape
7.1/7.2, Mozilla 1.4 or later, Firefox do with
textControl.value.substring(textControl.selectionStart,
textControl.selectionEnd)
and IE/Win also does
document.selection.createRange().text
others like Opera don't offer an API.
 
L

lawrence

Martin Honnen said:
STB wrote:



It depends on whether the browser offers an API, some like Netscape
7.1/7.2, Mozilla 1.4 or later, Firefox do with
textControl.value.substring(textControl.selectionStart,
textControl.selectionEnd)
and IE/Win also does
document.selection.createRange().text
others like Opera don't offer an API.

Don't imitate this, because it is bad code, but in IE you can do
something like this:

function wrapSelectionBold (element) {
var range = document.selection.createRange();
if (range.parentElement() == element) range.text = '<b>' +
range.text + '<\/b>';
}

<input type="button" value="bold"
onclick="wrapSelectionBold(this.form.inputId)" />
 
T

Thomas 'PointedEars' Lahn

lawrence said:

Please do not post attribution novels.
We were recently discussing the issue here:
http://groups.google.com/groups?hl=...om&hl=en&lr=&ie=UTF-8&scoring=d&start=10&sa=N

1. The thread you point to does not answer the OP's question.
However, other threads do:

<http://groups.google.com/groups?as_...group=comp.lang.javascript&scoring=d&filter=0>

2. Mozilla Firefox and Venkman don't work well together, since Venkman was
designed for debugging with Mozilla Seamonkey; one should use that UA
instead.


PointedEars
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top