View Full Version : dynamically set readonly

01-22-2009, 02:15 AM
There are two ways for me to make a input field readonly:


In a JSP, I use <input type="text" id="test" readonly="readonly" value="1234abc" />

The field will become readonly--I can not edit the text in the field. This is what I wanted.

2) Dynamical:

But sometimes I need to achieve this at the runtime/dynamically:

<input type="text" value="1234abc" />

Now in the javascript:

document.getElementById("test").readonly = true;

The field will display as readonly. However, I can actually highlight the text, delete and edit it.

Why so and how can I solve this problem?



01-22-2009, 03:51 AM
Try this: :)

<title>Dynamic ReadOnly</title>
<!-- input type="text" id="test" readonly="readonly" value="1234abc" / -->
<input type="text" id="test" disabled value="1234abc" />
<button onclick="document.getElementById('test2').disabled=false">Reset ReadOnly</button>
<input type="text" id="test2" value="1234abc" />
<button onclick="document.getElementById('test2').disabled=true">Set ReadOnly</button>