How to check whether any text is selected in <textarea> element?

D

Disc Magnet

For text elements there is a select() method to select text. I am
trying to provide the user with this feature. If he clicks within a
<textarea> element, all text is selected. This is easy to do using the
select() method. But if he clicks again, all text should be
unselected.

For this to happen, I need to know whether any text is already
selected in <textarea>. How can this be achieved?
 
E

Evertjan.

Disc Magnet wrote on 22 jun 2010 in comp.lang.javascript:
For text elements there is a select() method to select text. I am
trying to provide the user with this feature. If he clicks within a
<textarea> element, all text is selected. This is easy to do using the
select() method. But if he clicks again, all text should be
unselected.

For this to happen, I need to know whether any text is already
selected in <textarea>.
Why?

How can this be achieved?

try:

<textarea onclick='this.select();'>
qwerty
</textarea>
 
D

Disc Magnet

Disc Magnet wrote on 22 jun 2010 in comp.lang.javascript:



Why?

Because if it is selected, I want to unselect the text. For that I
need to know the current state of selection.
try:

<textarea onclick='this.select();'>
qwerty
</textarea>

I tried this. But this doesn't work on IE and Firefox. It works only
on Chrome. On IE and Firefox, if I click on the textarea for the
second time, the text does not get unselected.
 
A

Asen Bozhilov

Disc said:
Because if it is selected, I want to unselect the text. For that I
need to know the current state of selection.

But why do you want such a behavior? That would break my normal
browsing and probably I will close your page. For example I click on
textarea and start writing, with your approach I will lose my entered
text. Another problem is when I change the position of a caret with
mouse click in textarea. What will be happen select/deselect? If you
explain why do you want that, probably there is another answer on your
question instead that terrible decision.
 
D

Disc Magnet

But why do you want such a behavior? That would break my normal
browsing and probably I will close your page. For example I click on
textarea and start writing, with your approach I will lose my entered
text. Another problem is when I change the position of a caret with
mouse click in textarea. What will be happen select/deselect? If you
explain why do you want that, probably there is another answer on your
question instead that terrible decision.

Many websites which generate code for the user to copy provide such a
functionality where the whole textarea is selected with a single
click. So, the user can click on the textarea and copy the text
quickly. This doesn't break normal browsing because users don't want
to edit the text. They simply want to copy the text. Such textarea
also usually have the readonly="readonly" attribute set.

Also, have a look at this example: http://delicious.com/help/tagometer
 
E

Evertjan.

Disc Magnet wrote on 22 jun 2010 in comp.lang.javascript:
Because if it is selected, I want to unselect the text. For that I
need to know the current state of selection.


I tried this. But this doesn't work on IE and Firefox. It works only
on Chrome. On IE and Firefox, if I click on the textarea for the
second time, the text does not get unselected.

Not exactly whay you want, but try:

<script type='text/javascript'>
var x=true;
</script>

<textarea onclick='if(x=!x)this.select();'>
qwerty
</textarea>
 
A

Asen Bozhilov

Disc said:
Many websites which generate code for the user to copy provide such a
functionality where the whole textarea is selected with a single
click. So, the user can click on the textarea and copy the text
quickly. This doesn't break normal browsing because users don't want
to edit the text.

The provided code is usually for developers and such a functionality
is not so quick way to copy anything. If the size of code is big I
would prefer to use subversion account or download link.
They simply want to copy the text. Such textarea
also usually have the readonly="readonly" attribute set.

No, you do not know which part of the text respectively source code
they want to copy. If I want to copy some parts of your code, your
approach will break that.
Also, have a look at this example:http://delicious.com/help/tagometer

Have a look there: <URL: http://www.javascriptkit.com /> for
"solution" of your problem.
 
D

Disc Magnet

The provided code is usually for developers and such a functionality
is not so quick way to copy anything. If the size of code is big I
would prefer to use subversion account or download link.


No, you do not know which part of the text respectively source code
they want to copy. If I want to copy some parts of your code, your
approach will break that.


Have a look there: <URL:http://www.javascriptkit.com/> for
"solution" of your problem.

Asen, WTF are you talking about? If you don't have anything useful to
say, why don't you simply shut up!
 
D

Disc Magnet

Asen's objections were absolutely justified. I looked at the example you
gave (the "tagometer" widget), and found the autoselect "feature" very
annoying. Developers usually already know how to copy and paste code.

If you find that annoying, it's your problem. Moreover, I didn't come
where with a question about whether I should have this feature. I came
here with a technical question. I didn't want this offtopic and silly
discussion.

"Developers usually already know how to copy and paste code" is a lame
argument against such a feature. It is meant to make the job of the
developer easier. Instead of two clicks, he needs just one click.
The select/unselect functionality you suggested doesn't improve
anything; instead it interferes with your visitors' ability to select
and copy only part of a code example.

It improves in many people's opinion. A part of the widget code is
mostly useless. Most developers would copy the complete code as-is.
Yes, there might be a few developers who would want to copy only a
part of it. I would rather cater to most developers than these few
developers who would want to copy only a part of it for their own
reasons. Again, I need not advice from you in this regard. I know what
I am doing.
And finally, if you want to get help in this group, attacking people
without provocation is probably not a very clever move.

No, thanks. With idiots like you lurking around who don't care to
answer the technical question but instead go on a off-topic rant about
what is annoying and what is pleasing, I don't want to get help in
this group any more. IRC is a far better and more intelligent place.
 
R

rf

Disc said:
If you find that annoying, it's your problem. Moreover, I didn't come
where with a question about whether I should have this feature. I came
here with a technical question. I didn't want this offtopic and silly
discussion.

"Developers usually already know how to copy and paste code" is a lame
argument against such a feature. It is meant to make the job of the
developer easier. Instead of two clicks, he needs just one click.


It improves in many people's opinion. A part of the widget code is
mostly useless. Most developers would copy the complete code as-is.
Yes, there might be a few developers who would want to copy only a
part of it. I would rather cater to most developers than these few
developers who would want to copy only a part of it for their own
reasons. Again, I need not advice from you in this regard. I know what
I am doing.


No, thanks. With idiots like you lurking around who don't care to
answer the technical question but instead go on a off-topic rant about
what is annoying and what is pleasing, I don't want to get help in
this group any more. IRC is a far better and more intelligent place.

Don't let the door hit you in the arse on your way out.
 
G

Garrett Smith

Who?

[...]

It improves in many people's opinion. A part of the widget code is
mostly useless. Most developers would copy the complete code as-is.

For the vast majority of us who know how to select and deselect text,
the U/X provided in that example is a bug.
Yes, there might be a few developers who would want to copy only a
part of it. I would rather cater to most developers than these few
developers who would want to copy only a part of it for their own
reasons. Again, I need not advice from you in this regard. I know what
I am doing.


No, thanks. With idiots like you lurking around who don't care to

I see condescending statements about Stephen and Asen from a poster
"Disc Magnet" whose only posts to date demonstrated a bad idea, bad
implementation, and insults.

I can't see how such statements about Stephen and Asen follow. I can't
figure out any good motivation for making them. If your goal is to
pollute this NG with off-topic flames and retaliation, you've succeeded.

You're the new poster boy for Yahoo developers. Yesterday it was Zakas,
but today it's you. What is your name, son?

Garrett
 
D

Disc Magnet

On 22/06/10 23:33, Disc Magnet wrote:
Disc Magnet wrote:
Who?
They simply want to copy the text. Such textarea
also usually have the readonly="readonly" attribute set.
[...]

It improves in many people's opinion. A part of the widget code is
mostly useless. Most developers would copy the complete code as-is.

For the vast majority of us who know how to select and deselect text,
the U/X provided in that example is a bug.
Yes, there might be a few developers who would want to copy only a
part of it. I would rather cater to most developers than these few
developers who would want to copy only a part of it for their own
reasons. Again, I need not advice from you in this regard. I know what
I am doing.
No, thanks. With idiots like you lurking around who don't care to

I see condescending statements about Stephen and Asen from a poster
"Disc Magnet" whose only posts to date demonstrated a bad idea, bad
implementation, and insults.

I can't see how such statements about Stephen and Asen follow. I can't
figure out any good motivation for making them. If your goal is to
pollute this NG with off-topic flames and retaliation, you've succeeded.

You're the new poster boy for Yahoo developers. Yesterday it was Zakas,
but today it's you. What is your name, son?

Garrett

WOW! This group is amazing. Why don't you guys get a life! :p
Seriously, you guys care to participate in an offtopic flame war?
 
M

Michael Haufe (\TNO\)

For text elements there is a select() method to select text. I am
trying to provide the user with this feature. If he clicks within a
<textarea> element, all text is selected. This is easy to do using the
select() method. But if he clicks again, all text should be
unselected.

For this to happen, I need to know whether any text is already
selected in <textarea>. How can this be achieved?

Here's a vague constructive answer: If the element hasFocus, you could
it is highlighted then blur() it, otherwise select() it. To implement
hasFocus you'd have to capture the relevant events of the keyboard
(using the tab key) and the mouse (onclick) which would signify it
gaining focus (assuming this is only for the commonly used desktop
browsers).

Alternatively you may be able to use the text range methods of the
specific browsers in question to determine if anything is highlighted
currently:
http://www.quirksmode.org/dom/range_intro.html

The latter approach would be more difficult to get working properly
IMO.
 
L

Lasse Reichstein Nielsen

Disc Magnet said:
For text elements there is a select() method to select text. I am
trying to provide the user with this feature. If he clicks within a
<textarea> element, all text is selected. This is easy to do using the
select() method. But if he clicks again, all text should be
unselected.

For this to happen, I need to know whether any text is already
selected in <textarea>. How can this be achieved?

This is the behavior I get from simply calling select() on the focus
event.

<textarea onfocus="select();" readonlot>
Argle
Bargle
</textarea>

When I first click the textarea, the contents are selected.
When I then click again inside the textarea, the selection is removed again.

Does this behave like what you need, or is there a difference?

(Tested in Opera and IE).
/L
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top