Skip to content

Instantly share code, notes, and snippets.

@kswedberg
Created August 27, 2010 15:24
Show Gist options
  • Save kswedberg/553566 to your computer and use it in GitHub Desktop.
Save kswedberg/553566 to your computer and use it in GitHub Desktop.
var elem = document.createElement("div"),
style = elem.style,
userSelectProp = "userSelect" in style && "userSelect";
if (!userSelectProp) {
$.each("Moz Webkit Khtml O".split(" "), function(i, v) {
var vendorProp = v + "UserSelect";
if ( vendorProp in style ) {
userSelectProp = vendorProp;
return false;
}
});
}
var selectStart = !userSelectProp && "onselectstart" in elem && "selectstart.mouse";
elem = null;
$.fn.extend({
disableSelection: function() {
if (unselectableProp) {
this.css(userSelectProp, "none");
} else {
this.find( "*" ).andSelf().attr( "unselectable", "on" );
}
if (selectStart) {
this.bind(selectStart, function() {
return false;
});
}
return this;
},
enableSelection: function() {
if (unselectableProp) {
this.css(userSelectProp, "");
} else {
this.find( "*" ).andSelf().attr( "unselectable", "off" );
}
if (selectStart) {
this.unbind(selectStart);
}
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment