0

I've been trying to find a way to fully prevent text input text selection (Chrome, v. 104)

#testInput {
  user-select: none;
  pointer-events: none;
  width: 100%;
}
<!DOCTYPE HTML>
<html>
  <label for="testInput">Input label</label>
  <input id="testInput" type="text" value="Test value">
</html>

It is still possible to select text inside the input. Either through selecting the end of the label text and dragging mouse through the input, control + a and other ways. user-select also seems to have no effect on input element, the only reason copying directly is impossible is due to the pointer-events: none css setting.

https://jsfiddle.net/a69bswmq/

1
  • You can use a Stack Snippet to post the executable fiddle here, instead of a link to jsfiddle. Commented Aug 27, 2022 at 21:45

1 Answer 1

0

Although it's possible for someone to look at the source code and copy the text, here are some JS hacks that will make the copying a little bit harder and prevent non-developers to copy the text:

document.getElementById('testInput')
.addEventListener('selectionchange ', function (e) {
  window.getSelection().empty();
  window.getSelection().removeAllRanges();
});

// Detect Ctrl/Cmd+C and temporarily switch the value
document.addEventListener("keydown", e =>{
  const input = document.getElementById('testInput');
  const oldVal = input.value;
  if ( (e.ctrlKey || e.metaKey ) && ( e.key === "c" || e.code === "KeyC") ){
    input.value = "DON'T COPY";
    setTimeout(()=>{
      input.value = oldVal;
    }, 1000)
  }
})

// Disable Right-Click and Copy
document.addEventListener("contextmenu", e =>{
    const selection = window.getSelection();
    if ( selection && ( selection.type === 'Range' ) ){
      e.preventDefault();
    }
})
#testInput {
  user-select: none;
  pointer-events: none;
  width: 100%;
}
<label for="testInput">Input label</label>
<input id="testInput" type="text" value="Test value">

This is just a simple example to get you started. We can track other events also (drag, selection, mousedown, etc.) and make this even harder for someone to copy the text ...unless they open the DevTools and take a peek at the input value. ¯_(ツ)_/¯

Another approach would be to super-impose another element on top of the input element so that when the user tried to copy the text, they would be interacting with another element instead of the input element containing the value.

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

4 Comments

Thank you for the exhausting answer, although understandably you've come to the conclusion the point is text copying, it is not the point of my question. The current implementation is sufficient for my purposes, as you said, it's always possible to just copy the HTML source cod anyway. What bothers me is purely the fact that I can select the text and the different behavior from other text elements, which do not work the same way, the inconcistent behavior
You can use user-select: none to prevent selecting text from the whole page text if you like: * { user-select: none; }
But, yes, I get your point. There's a reason, I guess, why the implementation turned out this way in the browsers. We have to think about all sorts of cases.
No no, what I meant is inconsistent behavior between different HTML elements. There is no way to select a text from a e.g. paragraph element if the user-select is set to none. Basically it's just an "OCD issue". If I disable user-select for the whole page, it makes it harder to select the text in the label, however it is still possible + it is not my intention to make everything on the document not selectable.

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.