PDA

View Full Version : JS Unselect text



fasterthanlight™
April 26th, 2010, 03:58 PM
Yo.

Check this thing I've mashed together,

www.akaibo.com/k-test.html

This is actually styled really nicely, I just removed all that stuff for obvious reasons.

The problem is, when you click a thing, and it slides, and then you click it again and it slides back, if you click it again really quickly after that, you end up selecting the text "Me"

Does that make sense??

Just click around a whole bunch and you will eventually highlight the text "Me" -- and I want to disable this somehow.

I am having trouble finding anything online, as all my search queries return stuff that is labeled as the thing I'm looking for, but ISN'T the thing I'm looking for.

$("#mydiv").double click(function() {
unselect all text!
}


Help!?

skullsnest
April 26th, 2010, 11:55 PM
try this function out




$("#mydiv").double click(function() {
var sel ;
if(document.selection && document.selection.empty){
document.selection.empty() ;
} else if(window.getSelection) {
sel=window.getSelection();
if(sel && sel.removeAllRanges)
sel.removeAllRanges() ;
}
}

fasterthanlight™
April 29th, 2010, 01:51 PM
That didn't work, but it doesn't matter because I realize now that this can be accomplished with simple CSS:

#element {
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}

icio
April 29th, 2010, 03:09 PM
The following idea is based on my thinking that you probably shouldn't mess with the text that the user is trying to select (as you would do if you tried to circumvent their doing so accidentally.)

You could create an overlay for the whole clickable area, separated appropriately to act as if it were individual items. Give the overlays the click handlers and pass mouseover, mouseout and mousemove events (as you feel is appropriate) to the moving parts for them to know when (not) to show their hover state.

I think this might work because double-clicking ought to select the text of the element you're clicking on. Thus if you double-click on a text-less element it shouldn't do anything, right?

Hope that helps :thumb:

fasterthanlight™
June 10th, 2010, 02:28 AM
one month later

Thats a pretty abstract idea, whoa!
Normally I would agree with you in terms of not mucking about with what a user can and can't select, but as luck would have it, this widget I coded contains text that no user would ever want to copy and paste, just because it performs a very specific function.

It was strange though, when I was double clicking, it would select a word and a half of text, not like, the whole element that is the text.

Hrm.

Swooter
June 10th, 2010, 10:19 AM
Maybe this will work?

ondblclick="return false"

OR

ondblclick="this.blur()"

Nephs
April 13th, 2012, 03:02 PM
Try document. getSelection(). removeAllRanges()