I have a table, and I'm allowing users to 'select' multiple rows. This is all handled using jQuery events and some CSS to visually indicate the row is 'selected'. When the user presses shift, it is possible to select multiple rows. Sometimes this cause text to become highlighted. Is there anyway to prevent this?
-
possible duplicate of [CSS rule to disable text selection highlighting](http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting) – Dan Jul 18 '13 at 13:59
7 Answers
There is a CSS3 property for that.
#yourTable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

- 35,104
- 14
- 75
- 93
-
20
-
1@NathanTaylor After googling you will find a duplicate where this is already solved: http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting – Dan Jul 18 '13 at 13:59
-
`Validation (CSS 3.0): "moz-none" is not a valid value for the "-moz-user-select" property.` And indeed, this didn't work for Firefox until I changed `moz-none` to `none`. – Nolonar Oct 29 '14 at 12:19
If you want to have control when your users can select or not parts of you site, you can use this little jQuery plugin.
jQuery.fn.extend({
disableSelection : function() {
return this.each(function() {
this.onselectstart = function() { return false; };
this.unselectable = "on";
jQuery(this).css('user-select', 'none');
jQuery(this).css('-o-user-select', 'none');
jQuery(this).css('-moz-user-select', 'none');
jQuery(this).css('-khtml-user-select', 'none');
jQuery(this).css('-webkit-user-select', 'none');
});
}
});
and use it as:
// disable selection on #theDiv object
$('#theDiv').disableSelection();
Otherwise, you can just use these css attributes inside your css file as:
#theDiv
{
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

- 17,663
- 13
- 46
- 59
Just one note on the answer from Cleiton above - the code sample seems to work well, but in order to be a good citizen in the jQuery world, you should return the jQuery object at the end of the function so that it's chainable - a simple one-line addition fixes that up:
jQuery.fn.extend({
disableSelection : function() {
this.each(function() {
this.onselectstart = function() { return false; };
this.unselectable = "on";
jQuery(this).css('-moz-user-select', 'none');
});
return this;
}
});
Cheers, hope this is helpful.

- 339
- 3
- 8
I simply remove the selection that is made using the shift key. This might show a little flicker though
if (event.shiftKey) {
window.getSelection().removeAllRanges()
}

- 13,293
- 3
- 40
- 45
You can try with focus() function on the selected text - the selection dissapears.
$('#someEl').focus();

- 558
- 4
- 11
If you have Jquery UI on your pages, just use
$("element-selector").disableSelection();

- 7,713
- 2
- 31
- 36
-
1This is undocumented but should work if you have jQuery UI core – Shadrack Orina Nov 04 '11 at 20:26
-
good thing about this solution is that its also applicable to IE and replicate the functionality of solutions already mentioned if you look at the code. – sksallaj Feb 12 '13 at 14:03
-
1
::-moz-selection { /* Code for Firefox */
color: black;
background: none;
}
::selection {
color: black;
background: none;
}
from http://www.w3schools.com/cssref/sel_selection.asp
actually from the try-it section, after changing the values.
notice though that the cursor is still 'I' shaped...
-
Note: The ::selection pseudo-element was drafted for CSS Selectors Level 3, but removed before the Recommendation status. So, at the moment, the ::selection pseudo-element is not in any specification. (However, it may be re-added to future CSS specifications.) – luk2302 May 30 '15 at 18:50