Reputation: 39
I am trying to prevent selection on an input field with the following:
considerations
So far I have tried these things: The id is work well but i want to tag name.
HTML
<p class="noselect">
Unselectable text.
<input class="my_input" type="text" value="Try to select me!">
</p>
JS
var inp = document.getElementByClassName('my_input');
inp.addEventListener('select', function() {
this.selectionStart = this.selectionEnd;
}, false);
Upvotes: 0
Views: 185
Reputation: 123
There are minor corrections in given code snippet.
var inp = document.getElementsByClassName('my_input');
inp[0].addEventListener('select', function() {
this.selectionStart = this.selectionEnd;
}, false);
Upvotes: 1