473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Apr 20 '07 #1
6 19696
"Fnord Nase" <fn*******@gmail.comwrote in message
news:11**********************@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
Apr 20 '07 #2
On Apr 20, 5:06 pm, "-Lost" <missed-s...@comcast.netwrote:
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 <inputfield and to select()
its contents, but this is really just an ugly hack...

cheers,
nase

Apr 20 '07 #3
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.
Apr 20 '07 #4
On Apr 20, 8:21 pm, Elegie <ele...@invalid.comwrote:
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

Apr 20 '07 #5
On Apr 21, 4:21 am, Elegie <ele...@invalid.comwrote:
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

Apr 21 '07 #6
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.
Apr 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yair | last post by:
hey all i have a page with a table, and when the user doubleclicks a row in it, a window is opened. The problem is, if he doubleclicked a text in the row, it's becoming selected. what i want is...
4
by: perspolis | last post by:
hi I manage a double click event in a combo box.. but this event doesn't fire ???? I don't know why??
3
by: Grant | last post by:
I am wondering what is the surest way to prevent user from double clicking on a button and in this case it is a deposit button. Sometime the user is impatient after clicking on the button so they...
3
by: Anders K. Jacobsen [DK] | last post by:
Hi im building a administraive application (primary) CRUD. But how do i prevent users not to double click on eg a insert button? I would be great the have uniform way to deal with this since I...
3
by: Neil Wallace | last post by:
Hi, This is an odd one. I've been struggling to get "double click" to work well for my controls. The same event handler works perfectly for buttons, but not for labels. Can anyone tell me...
4
by: bkasmai | last post by:
On editing textboxes on a webform, when a user tab to a textbox, the text is highlighted or selected. Any accidental key press will remove the original text. This has proved a problem for the...
4
by: Jeff User | last post by:
Hi Using .NET 1.1, C#, web app I (actually our client) would like to be able to double click a selection in a listbox and have it postback to server . There I would want to access the item that...
4
by: PJ6 | last post by:
Is there a way to prevent the double-click event on an element from causing IE6 to select everything? Cancelling the event doesn't work. Paul
6
by: Jim Devenish | last post by:
I have an unbound form that displays all the days of the year as a calendar. It has 12 rows of text boxes with either 29,30 or 31 in each row. Text box names are of the form: display_01_01,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.