3

I have a basic table, like this:

<table>
    <tbody>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
        </tr>
    </tbody>
</table>

On the rows I have a double click function with jQuery:

$('tr').live('dblclick',function(){
    dosomething();
    return false;
});

I also have a plugin called tablednd that uses mousedown/up function on the rows. The double clicking causes text selection on the cells.

How can I prevent this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
passatgt
  • 4,234
  • 4
  • 40
  • 54

2 Answers2

11

You can't use select() event because it is limited to input elements.

Instead, try preventDefault() on the selectstart event...

$('tr').bind('selectstart', function(event) {
    event.preventDefault();
});

jsFiddle.

Alternatively, you can use CSS...

tr {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks, it works on the normal browsers, but no ie8 (if i disable the tablednd plugin, its working on ie8 too) – passatgt Apr 10 '11 at 11:28
  • @passatgt [See this answer as well](http://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click/880518#880518). – alex Apr 10 '11 at 11:41
0

Try changing

$('tr').live('dblclick',function(){
    dosomething();
    return false;
});

to

$('tr').live('dblclick',function(e){
    e.preventDefault();
    dosomething();
    return false;
});

this should prevent the doubleclick from doing anything the browser will do at default. You should test this code in all browsers though, not sure if it will work everywhere.

teuneboon
  • 4,034
  • 5
  • 23
  • 28