Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Web - Disable long-touch/taphold text selection

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!

like image 537
Brad Orego Avatar asked Jun 28 '12 04:06

Brad Orego


People also ask

How do I turn off text selection on a web page?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.

How do I stop my text from highlighting?

Remove highlighting from part or all of a document Select the text that you want to remove highlighting from, or press Ctrl+A to select all of the text in the document. Go to Home and select the arrow next to Text Highlight Color. Select No Color.


2 Answers

-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.

like image 101
CoreyRS Avatar answered Sep 16 '22 19:09

CoreyRS


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.

like image 45
arttronics Avatar answered Sep 17 '22 19:09

arttronics