Prevent text selection after double click

F

Fnord Nase

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
 
L

-Lost

Fnord Nase said:
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
 
F

Fnord Nase

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
 
E

Elegie

Fnord said:
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.
 
F

Fnord Nase

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
 
R

RobG

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

Elegie

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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top