If you want to remove the selection almost instantly when someone selects something inside an input element, you can use
.getSelection().removeAllRanges();
.
Copying the text is still possible if someone is quick.
$("input").select(function() {
window.getSelection().removeAllRanges();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" value="123">
To make it appear as though the text is not selectable (as suggested in Samuel's answer), you can utilize this additional code snippet:
$("input").select(function() {
window.getSelection().removeAllRanges();
});
input::selection {
background-color:transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" value="123">
In addition to these methods, you can prevent the user from copying anything within an input field by adding onCopy="return false"
to the input field. Although it remains selectable, copying is disabled.
<input type="number" value="123" onselectstart="return false" oncopy="return false;" />
You may choose to implement one or a combination of two of these techniques in your project. By combining the necessary elements, you can create an input field that is both unselectable and uncopyable.
You can also explore disabling cutting/pasting or right-clicking, as explained in this article.