32

I've seen/heard all about disabling text selection with the variations of user-select, but none of those are working for the problem I'm having. On Android (and I presume on iPhone), if you tap-and-hold on text, it highlights it and brings up little flags to drag and select text. I need to disable those (see image):

screenshot

I've tried -webkit-touch-callout to no avail, and even tried things like $('body').on('select',function(e){e.preventDefault();return;}); to no avail. And the cheap tricks like ::selection:rgba(0,0,0,0); won't work either, as hiding these won't help - selection still happens and it disrupts the UI. Plus I'm guessing those flags would still be there.

Any thoughts would be great. Thanks!

Brad Orego
  • 2,976
  • 3
  • 18
  • 24
  • Hmmm. The Fiddle seemed to work when I just tested it on my phone, but I'm not 100% sure it would work for me in production - I still need to be able to trigger events (notably, `touchstart`, `touchmove`, and `touchend`) on the elements. Although, considering this is only acting on `selectstart`, we might be good. I'll test it out tonight and get back to you :) – Brad Orego Jul 06 '12 at 17:44

3 Answers3

69
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
-webkit-tap-highlight-color:rgba(0,0,0,0);

This will disable it for every browser going.

CoreyRS
  • 2,227
  • 3
  • 26
  • 44
  • 1
    Not to be mean, but did you read the question at all?? I have exactly that plastered all over my CSS (from individual IDs/classes all the way up to the body and `*`), and I explicitly stated hiding the highlighting is a bad idea.... – Brad Orego Jun 28 '12 at 04:42
  • 9
    Yes, I did read your post. Now, did you actually try the code? It has -webkit-touch-callout added and the correct way to prevent the highlight overlay using -webkit-tap-highlight-color. I use this in my webapps and it works fine - no content menu, no highlighting and no selection. – CoreyRS Jun 28 '12 at 05:19
  • 2
    I suppose I didn't mention originally, but I've had `-webkit-touch-callout:none;` in there the entire time as well. The only thing `-webkit-tap-highlight-color` would do would be make the highlight transparent, __not__ disable it. I didn't bother with the code initially because it wasn't anything I hadn't seen before, but just to appease you I tried it and it didn't work. What OSes/devices have you tested on that it worked? Not working on Android 2.3 or 3.0... – Brad Orego Jun 28 '12 at 05:30
  • Thanks! Worked great for me. On Android 4.4.4 / Chrome – CodeBrauer Aug 22 '14 at 09:26
  • Best way to do it - pure CSS, no extra JS or plugins. – Bud Damyanov Aug 09 '18 at 08:14
16

Reference:

jsFiddle Demo with Plugin

The above jsFiddle Demo I made uses a Plugin to allow you to prevent any block of text from being selected in Android or iOS devices (along with desktop browsers too).

It's easy to use and here is the sample markup once the jQuery plugin is installed.

Sample HTML:

<p class="notSelectable">This text is not selectable</p>

<p> This text is selectable</p>

Sample jQuery:

$(document).ready(function(){

   $('.notSelectable').disableSelection();

});

Plugin code:

$.fn.extend({
    disableSelection: function() {
        this.each(function() {
            this.onselectstart = function() {
                return false;
            };
            this.unselectable = "on";
            $(this).css('-moz-user-select', 'none');
            $(this).css('-webkit-user-select', 'none');
        });
        return this;
    }
});

Per your message comment: I still need to be able to trigger events (notably, touchstart, touchmove, and touchend) on the elements.

I would simply would use a wrapper that is not affected by this plugin, yet it's text-contents are protected using this plugin.

To allow interaction with a link in a block of text, you can use span tags for all but the link and add class name .notSelected for those span tags only, thus preserving selection and interaction of the anchors link.

Status Update: This updated jsFiddle confirms you concern that perhaps other functions may not work when text-selection is disabled. Shown in this updated jsFiddle is jQuery Click Event listener that will fire a Browser Alert for when the Bold Text is clicked on, even if that Bold Text is not text-selectable.

anon
  • 4,578
  • 3
  • 35
  • 54
arttronics
  • 9,957
  • 2
  • 26
  • 62
  • This does NOT work for my Android 2.3.3,,, Just went to the your jsFiddle and tried it out. Selected all of the text. Any ideas? – Oneezy Feb 20 '13 at 09:29
  • Thanks for the feedback. After the JsFiddle webpage has loaded, hit the RUN button before testing. Also, while you technically can select all the text, doing a copy of that and pasting into the next document will not paste the blocked portion of text. If still this is an issue, I will try to access a Android device with that version number. Thoughts? – arttronics Nov 15 '13 at 05:02
  • +1 Fantastic, thanks @arttronics! What variation might prevent the context menu from showing on "long touch"? – Ian Campbell Jun 15 '14 at 05:48
2

-webkit-user-select:none; wasn't supported on Android until 4.1 (sorry).

John Reck
  • 341
  • 2
  • 4