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?
Select the text that you want to remove highlighting from, or press Ctrl+A to select all of the text. Go to Home and select the arrow next to Text Highlight Color. Select No Color.
Use the user-select: none; CSS rule.
Answer: Use the CSS ::selection pseudo-element By default, when you select some text in the browsers it is highlighted normally in blue color. But, you can disable this highlighting with the CSS ::selection pseudo-element.
Try applying user-select: none to an element and dragging over it.
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; }
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With