Prevent text selection after double click

Discussion in 'Javascript' started by Fnord Nase, Apr 20, 2007.

  1. Fnord Nase

    Fnord Nase Guest

    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
     
    Fnord Nase, Apr 20, 2007
    #1
    1. Advertising

  2. Fnord Nase

    -Lost Guest

    "Fnord Nase" <> wrote in message
    news:...
    > 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
     
    -Lost, Apr 20, 2007
    #2
    1. Advertising

  3. Fnord Nase

    Fnord Nase Guest

    On Apr 20, 5:06 pm, "-Lost" <> 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
     
    Fnord Nase, Apr 20, 2007
    #3
  4. Fnord Nase

    Elegie Guest

    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.
     
    Elegie, Apr 20, 2007
    #4
  5. Fnord Nase

    Fnord Nase Guest

    On Apr 20, 8:21 pm, Elegie <> 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
     
    Fnord Nase, Apr 21, 2007
    #5
  6. Fnord Nase

    RobG Guest

    On Apr 21, 4:21 am, Elegie <> 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
     
    RobG, Apr 21, 2007
    #6
  7. Fnord Nase

    Elegie Guest

    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.
     
    Elegie, Apr 21, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders K. Jacobsen [DK]

    Prevent double click

    Anders K. Jacobsen [DK], Mar 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    5,262
    Steve C. Orr [MVP, MCSD]
    Mar 14, 2005
  2. Sydex
    Replies:
    12
    Views:
    6,654
    Victor Bazarov
    Feb 17, 2005
  3. Replies:
    4
    Views:
    735
  4. PJ6
    Replies:
    4
    Views:
    915
  5. yair

    disable double-click selection

    yair, Jul 16, 2003, in forum: Javascript
    Replies:
    1
    Views:
    125
Loading...

Share This Page