I am seeking to disable selection on an input field while still allowing for focusing, with the following criteria:
- Preventing selection via mouse
- Preventing selection via keyboard (including Ctrl+A, shift+arrows)
- Permitting focus on the field using both keyboard and mouse
My attempts so far have included:
CSS
I have tried applying the following class:
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Outcome: Selection is still possible.
I have also experimented with the below code without success:
$("#my_field").attr('unselectable','on')
.css({'-moz-user-select':'-moz-none',
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none',
'-webkit-user-select':'none',
'-ms-user-select':'none',
'user-select':'none'
}).bind('selectstart', function(){ return false; });
Javascript
I attempted the following:
$( "#my_field" ).select(function(e) {
console.log("Select called");
e.preventDefault();
});
Outcome: The message was printed in the console, but selection still occurred.
Thank you for any assistance provided.