Skip to content

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(); });

Answer

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!"/>