How to Disable Text Selection with CSS or JavaScript

CSS disable text selection

Don't apply these properties to the whole body. Move them to a class and apply that class to the elements you want to disable select:

.disable-select {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

How do I disable text selection with CSS or JavaScript?

<div 
style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;"
unselectable="on"
onselectstart="return false;"
onmousedown="return false;">
Blabla
</div>

CSS or Javascript to disable text selection highlighting from CTRL + A

Possible Duplicate.

If you are allowed to use jquery, here is the answer you are looking for.

65 is ascii for 'A' and 97 for 'a' if you want to account for both.

$(function(){   
$(document).keydown(function(objEvent) {

if (objEvent.ctrlKey) {
if ($(objEvent.target).not('input')){
if (objEvent.keyCode == 65 || objEvent.keyCode == 97) {
objEvent.preventDefault();
}
}
}
});
});

edit: modified based on your comment to allow for input.

edit 2: if you include jQuery UI you can use disableSelection() instead of disableTextSelect()
otherwise try this.

edit 3: Sorry, disableSelection() is deprecated in jQuery 1.9 see here.. Try this simple hack instead of disableSelection(). replace objEvent.disableTextSelect(); with objEvent.preventDefault(); above.

edit 4: made it a bit cleaner.

How to disable text selection on touch press?

Use this in your CSS..

 -webkit-touch-callout: none; /* Safari */
-webkit-user-select: none; /* Chrome */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none;

Disable text selection dynamically per click via javascript?

You can deselect all text with javascript; add a call to RemoveSelection() after you run the multi-select logic.

From http://help.dottoro.com/ljigixkc.php via Clear a selection in Firefox

function RemoveSelection () {
if (window.getSelection) { // all browsers, except IE before version 9
var selection = window.getSelection ();
selection.removeAllRanges ();
}
else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange ();
document.selection.empty ();
}
}
}


Related Topics



Leave a reply



Submit