Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / jQuery: How to prevent active input field from being changed?

how do I prevent the user to change the value in an input field (which contains a certain value to be copied into the clipboard) without using disabled="true"? The text should be selected once the user clicks in the field (that's working already) but entering anything should have no effect.

Thanks

jQuery('input.autoselect[value]').focus(function() { jQuery(this).select(); });
like image 622
Ole Spaarmann Avatar asked Jul 03 '09 13:07

Ole Spaarmann


People also ask

How do you prevent a user from typing in an input field?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

How to make an input field not editable in html?

The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control. If the readonly attribute is specified on an input element, because the user can not edit the input, the element does not participate in constraint validation.

How Stop focus in jQuery?

In jQuery by using blur() property we can remove focus from input textbox field.

How do you remove input field focus?

The blur() method removes focus from an element.


1 Answers

readonly in the html of the input is all you need to prevent the user from editing the input.

<input readonly="readonly" type="text" value="You can't edit me!"/>
like image 56
scunliffe Avatar answered Oct 18 '22 02:10

scunliffe