views:

56

answers:

1

I want to double click on a div, but right now it "selects"/highlights a word. How do I make it so that this doesn't happen?

I tried:

$(this).hide().show()

and 

$(this).blur()

But it still highlights the word.

+4  A: 

You can prevent an element from being selected in most browsers like this:

elem.onselectstart = function() { return false; }; 
elem.unselectable = "on"; 
$(elem).css({ "-moz-user-select": 'none', "-webkit-user-select": 'none' }); 

You could try doing that in the click event and undoing it one or two seconds later using setTimeout.

SLaks
shouldn't selectstart do an `e.preventDefault` here?
kibibu
@kibibu: Why bother?
SLaks
kibibu: No. `return false` is all you need for event handlers assigned via the "DOM0" method, i.e. assigning the event handler function to the appropriate property. This works in all browsers. The `preventDefault()` of the event only comes into play for event handlers assigned via `addEventListener()`.
Tim Down
I did this and it works in Firefox. But how do I make it work for IE?
TIMEX
alex: `elem.unselectable = "on";` should be doing that for you. However, if `elem` has children, you need to set `unselectable` to "on" for them too, and likewise for their children. Writing a function to do this recursively helps.
Tim Down