22

I am trying to prevent selection on an input field with the following considerations

  • Prevent selection using mouse
  • Prevent selection using keyboard (including Ctrl+A, shift+arrows)
  • Allow focusing into field using keyboard and mouse

So far I have tried these things:

CSS

I have attempted the using 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;
}

Result: selection still possible

I have also tried below with no 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 tried the below

$( "#my_field" ).select(function(e) {
        console.log("Select called");
        e.preventDefault();
});

Result: console printed the message, however the select still works

Thanks for your help

3
  • 1
    not really, those solutions don't apply to inputs Commented Mar 23, 2015 at 23:02
  • 1
    Have a look at stackoverflow.com/questions/15151908/… Commented Mar 23, 2015 at 23:07
  • @Joel that solution makes it look like its not being selected, however the selection still happens and I can drag that selection to another field Commented Mar 23, 2015 at 23:15

5 Answers 5

18

It can be done by setting the element's selectionStart to selectionEnd on select event:

var inp = document.getElementById('my_input');

inp.addEventListener('select', function() {
  this.selectionStart = this.selectionEnd;
}, false);
<input id="my_input" type="text" value="Try to select me!" />

Sign up to request clarification or add additional context in comments.

8 Comments

Prevent selection using keyboard —the above doesn't meet this criterion. Use tab to focus the field, which selects the entire content by default, so the value can then be copied. I don't know why anyone would want to do this anyway.
@RobG That's right, however we can do the same thing on focus event — jsbin.com/remihecomi/1/edit?js,output
@HashemQolami—it's still possible to select the content by starting the selection from outside the element. And in any reasonably modern browser I can inspect the element and copy the content from the inspector. Really, this sort of stuff is pointless.
@HashemQolami Selection is also possible, when starting selection from inside the input and stopping outside.
@OlivierKrull I couldn't reproduce it. Can you provide an online example?
|
6

You can cover over the <input> with any layer in absolute position, for example with div.wrapper::after or with other wrapper tag. Then use user-select: none for the wrapper tag (<div>, <label>, etc) and don't use user-select: none on the <input> itself.

But if your wrapper tag is <label> I recommend additionally adding the attribute readonly for the <input> and converting <label> to the block (or inline-block / flex / inline-flex).

input { outline: none; margin: 10px 0; width: 200px; }

.wrapper {
  position: relative;
  user-select: none;
}

.wrapper::after {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

label.wrapper { display: block; }
<div class="wrapper">
  <input type="text" value="Any text in the div wrapper"/>
</div>

<label class="wrapper">
  <input type="text" value="Any text in the label wrapper" readonly />
</label>

2 Comments

:D Thanks for the answer, I asked this more than 4 years ago. Hopefully, helps someone else
And even then: it sadly allows for selection using the keyboard (tab to highlight the input box, auto-selected on focus)
6

CSS-only solution:

This will not allow for input text to visually appear selected, while it is.
There are situations where this technique is good enough, if the developer do not care for the input's text to be able to be copy-pasted, but only the visual aspect of the selection is unwanted.

input{ color:transparent }
input::selection{ color:transparent }
<input value='invisible selection'>

It's also possible to use font-size:0 if width & height are defined on the input element.

JS Solution:

When the input gets focused → unfocus it (by calling the blur method):

<input value='unselectable selection' onfocus='this.blur()'>

1 Comment

input::selection {background-color: transparent} gives the perfect result
4

You can prevent the mousedown event to prevent the selection of the text inside the input.

<input type="text" id="unselectable" value="You can not select this text!"/>
<script>
document.getElementById("unselectable").addEventListener("mousedown", function(event){
  event.preventDefault();
});
</script>

1 Comment

This prevents the input to be focused by mouse click.
1

This is the solution for when if you want pointer icon with cursor.

.input-select {
  position: relative;
  user-select: none;
  cursor: pointer;
}

.input-select::after {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<div class="input-select">
  <input type="text" value="any"/>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.