Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Prevent text selection after double click (http://www.velocityreviews.com/forums/t930814-prevent-text-selection-after-double-click.html)

Fnord Nase 04-20-2007 02:41 PM

Prevent text selection after double click
 
Hi.

We're using the dblclick event in some parts of a web app interface,
which works okay, but when a user double clicks on a text element, the
browser selects some text before executing the event handler. How can
I prevent this behaviour?

TIA,
nase


-Lost 04-20-2007 03:06 PM

Re: Prevent text selection after double click
 
"Fnord Nase" <fnordnase@gmail.com> wrote in message
news:1177080067.234918.141740@l77g2000hsb.googlegr oups.com...
> Hi.
>
> We're using the dblclick event in some parts of a web app interface,
> which works okay, but when a user double clicks on a text element, the
> browser selects some text before executing the event handler. How can
> I prevent this behaviour?


I do not suppose you could tighten up the double click event to a particular element?

Say maybe make it fire only on buttons?

Otherwise, I do not believe it is possible to fire an event with a disabled FORM element.
Otherwise I would not know how to go about not highlighting when double clicking.

-Lost



Fnord Nase 04-20-2007 03:43 PM

Re: Prevent text selection after double click
 
On Apr 20, 5:06 pm, "-Lost" <missed-s...@comcast.net> wrote:
> Otherwise, I do not believe it is possible to fire an event with a disabled FORM element.
> Otherwise I would not know how to go about not highlighting when double clicking.


Oh, I wasn't talking about form elements. Sorry, I should been more
precise. The element that receives the dblclick event is a <div>
containing a few words (like "showing rows 11,024 to 11,090 of
129,776").

I guess my question is: is it possible to unselect text?
We had a similar situation previously, in a drag&drop context, and
IIRC we had to resort to using a hidden <input> field and to select()
its contents, but this is really just an ugly hack...

cheers,
nase


Elegie 04-20-2007 06:21 PM

Re: Prevent text selection after double click
 
Fnord Nase wrote:

> I guess my question is: is it possible to unselect text?


Yes you can, using Ranges and clearing the selection. See below (tested
IE7, FF2 and Opera 9).

---
<div ondblclick="clearSelection()">Hello, world!</div>
<script type="text/javascript">
function clearSelection() {
var sel ;
if(document.selection && document.selection.empty){
document.selection.empty() ;
} else if(window.getSelection) {
sel=window.getSelection();
if(sel && sel.removeAllRanges)
sel.removeAllRanges() ;
}
}
</script>
---

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp>
<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html>

Note that IE supports an onselectstart attribute; defining
onselectstart="return false" prevents any selection on the element.
However, you do not seem to want to prevent the selection in every case,
only when you have your double click behavior, so it might not be
appropriate to use it (after all, the user may legitimately want to
select some part of the element, for copy-pasting).


HTH,
Elegie.

Fnord Nase 04-20-2007 11:12 PM

Re: Prevent text selection after double click
 
On Apr 20, 8:21 pm, Elegie <ele...@invalid.com> wrote:
> function clearSelection() {
> var sel ;
> if(document.selection && document.selection.empty){
> document.selection.empty() ;
> } else if(window.getSelection) {
> sel=window.getSelection();
> if(sel && sel.removeAllRanges)
> sel.removeAllRanges() ;
> }}


Thanks a lot, Elegie!
That's exactly what I was trying to do.

cheers,
nase


RobG 04-21-2007 03:33 AM

Re: Prevent text selection after double click
 
On Apr 21, 4:21 am, Elegie <ele...@invalid.com> wrote:
> Fnord Nase wrote:
> > I guess my question is: is it possible to unselect text?

>
> Yes you can, using Ranges and clearing the selection. See below (tested
> IE7, FF2 and Opera 9).
>
> ---
> <div ondblclick="clearSelection()">Hello, world!</div>
> <script type="text/javascript">
> function clearSelection() {
> var sel ;
> if(document.selection && document.selection.empty){
> document.selection.empty() ;
> } else if(window.getSelection) {
> sel=window.getSelection();
> if(sel && sel.removeAllRanges)
> sel.removeAllRanges() ;


window.selection is DOM 0 and therefore not well documented. Removing
all ranges from a selection object may not necessarily remove all
selections, it doesn't in Safari at least. It might be better to use
the collapse method:

sel.collapse() ;

<URL: http://developer.mozilla.org/en/docs/DOM:Selection >


--
Rob


Elegie 04-21-2007 08:11 AM

Re: Prevent text selection after double click
 
RobG wrote:

Hello Rob,

> window.selection is DOM 0 and therefore not well documented. Removing
> all ranges from a selection object may not necessarily remove all
> selections, it doesn't in Safari at least. It might be better to use
> the collapse method:
>
> sel.collapse() ;
>
> <URL: http://developer.mozilla.org/en/docs/DOM:Selection >


Quite interesting. I wasn't aware of this collapse method on the
selection, to me the selection object would originally be different
different from a range object (which is why it offered methods to grab
and manipulate ranges from the selection, like removeAllRanges,
rangeCount, getRangeAt).

Thanks for the tip!

Regards,
Elegie.


All times are GMT. The time now is 08:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.