Read-only select elements in HTML

In HTML 4, form elements can be defined as “readonly” so that the user can’t modify the value. However, you’ll notice that select controls (the drop-down lists) do not support the readonly attribute, just input and textarea controls. Can anyone out there please explain the rational behind this? It seems perfectly reasonable to have a readonly select box, especially if it is put into that state by a script.

I think my workaround will consist of making them disabled so they cannot be editted, and enabling them via a script right before the form submits so that the values for those controls are submitted (disabled controls are not successful and are not “valid for submission”).

If anyone can explain why selects can’t be made readonly, or give me a better solution for simulating readonly-ness, please share.

This entry was posted in General and tagged , , . Bookmark the permalink.

4 Responses to Read-only select elements in HTML

  1. Anon says:

    You can first disable the select in the HTML code, then enable them with javascript.

    Then you add a handler to the onChange-event or something. This handler will set the selected index back to its original value (and also output an error message saying that you can’t change the values)

    You could also make it more obvious that it’s read only by using CSS and give the elements a more grey look

  2. josh says:

    You could leave it disabled and duplicate it with a hidden field.

  3. igo says:

    josh++

    this approch is very useful in lots of situations. for example – read only checkboxes (if checkbox is marked readonly you can still check/uncheck it – value of control IS read only, but UI state not :D ).

  4. Code Pug says:

    I have created a JavaScript snippet that will allow you to make your HTML select controls read-only simply by giving them an ID attribute and calling the script available on my site at http://www.codepug.com/readonlySelect.html

    Happy coding!
    –X
    .

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>