Sunday, March 23, 2008

Making a HTML select readonly

Source

For some time ago I needed to make a single select <select> element readonly. This meant that the user should be able to see all values in the list and not be able to select a new value. There is no readonly attribute available for the <select> element and disabling the field was not an option since we needed the value on the serverside. Some simple javascript did the job:


<select name="theselect" onchange="this.selectedIndex = 1;">
<option value="Red">Red</option>
<option value="Green" selected="selected">Green</option>
<option value="Blue">Blue</option>
</select>

Initial value is green and it should continue to be. This solution rely on the possibility to apply the correct onchange eventlistener during the page rendition - in this case through a taglib. I have created a simple test page for myself here.


1 comments:

lauren said...

I am new to Java Script so I do not have enough experience in this language. I am also facing similar problem but I tried the solution that you provided Now it achieves the requirement that the user should be able to see all values in the list and not be able to select a new value. Thanks for the sharing information.
electronic signatures