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

> On Jun 22, 11:15�pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net>
> wrote:
>> 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?
>
> Because if it is selected, I want to unselect the text. For that I
> need to know the current state of selection.
>
>>
>> > How can this be achieved?
>>
>> 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.

Not exactly whay you want, but try:

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

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



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Asen Bozhilov on
Disc Magnet wrote:

> 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.

From: Garrett Smith on
On 2010-06-22 11:33 PM, Disc Magnet wrote:
> On Jun 23, 4:16 am, Stefan Weiss<krewech...(a)gmail.com> wrote:
>> On 22/06/10 23:33, Disc Magnet wrote:
>>
>>
>>
>>
>>
>>> On Jun 23, 2:14 am, Asen Bozhilov<asen.bozhi...(a)gmail.com> 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.
>
>>
>> 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

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
From: Disc Magnet on
On Jun 23, 12:06 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> On 2010-06-22 11:33 PM, Disc Magnet wrote:
>
> > On Jun 23, 4:16 am, Stefan Weiss<krewech...(a)gmail.com>  wrote:
> >> On 22/06/10 23:33, Disc Magnet wrote:
>
> >>> On Jun 23, 2:14 am, Asen Bozhilov<asen.bozhi...(a)gmail.com>  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.
>
> >> 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
>
> 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?
From: "Michael Haufe ("TNO")" on
On Jun 22, 11:56 am, Disc Magnet <discmag...(a)gmail.com> wrote:
> 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.