tags:

views:

500

answers:

2

Doesn't oEvent.preventDefault(); work in GC? I need to prevent selecting text when the onmove event is triggered.

EDIT: It turns out to be very easy...

function disableSelection() {
   document.onselectstart = function() {return false;} // ie
   document.onmousedown = function() {return false;} // others
}
function enableSelection() {
   document.onselectstart = null; // ie
   document.onmousedown = null; // others
}

After that, there's no text selection triggered on the move event (ie. on the select event -- ie - indeed ie!)

+11  A: 

-webkit-user-select: none CSS style controls whether the user is allowed to select the
text of the element.

For completeness sake, this covers all supporting browsers (IE is not considered a web browser):

.no-select
{
   user-select: none;
   -o-user-select:none;
   -moz-user-select: none;
   -khtml-user-select: none;
   -webkit-user-select: none;
}

To do it through Javascript, just create a class and add the node's attributes.

LiraNuna
Very cool, I did not know you could do this through CSS.
musicfreak
I need to do it via JS.
Frank Furd
IE is not considered a web browser? How's that?
Frank Furd
Search in stackoverflow's question database, see how many 'Works on firefox, doesn't work on IE' questions you can find :)
LiraNuna
Bashing IE is so 2001.
kusma
You could add -o-user-select:none to the list for Opera support
Sandra
+2  A: 

To do it using JavaScript:

var el = document.getElementById("myElement"), s = el.style;
s.userSelect = "none";
s.webkitUserSelect = "none";
s.MozUserSelect = "none";
el.unselectable = "on"; // For IE and Opera

Note that for IE ands Opera, the unselectable property isn't inherited by an element's children, so any child elements of el will also need unselectable to be set to "on".

Tim Down
+1 for how to do it in IE.
Na7coldwater