How to make select option readonly+javascript

Posted: despairr Date of post: 31.05.2017

According to HTML specs, the select tag in HTML doesn't have a readonly attribute, only a disabled attribute. So if you want to keep the user from changing the dropdown, you have to use disabled. What's the best way to emulate the readonly attribute for a select tag, and still get the POST data?

You should keep the select element disabled but also add another hidden input with the same name and value. If you reenable your SELECT, you should copy its value to the hidden input in an onchange event and disable or remove the hidden input. This way the dropdown still works and submits its value but the user can not select another value.

This results in Select to be gray with nice "disable" cursor on hover and on select the list of options is "empty" so you can not change its value. Yet another more contemporary option no pun intended is to disable all the options of the select element other then the selected one.

Set the select disabled when you plan for it to be read-only and then remove the disabled attribute just before submitting the form. The code above disables all other options not selected while keeping the selected option enabled. Doing so the selected option will make it into the post-back data. In addition to disabling the options that should not be selectable i wanted to actually make them dissapear from the list, but still be able to enable them should i need to later:.

This finds all select elements with a readonly attribute, then finds all options inside those selects that are not selected, then it hides them and disables them. It is important to separate the jquery query in 2 for performance reasons, because jquery reads them from right to left, the code:. If you disable a form field, this won't be send when form is submitted. So if you need a readonly that works like disabled but sending values do this:. This is the simplest and best solution. You will set a readolny attr on your select, or anyother attr like data-readonly, and do the following.

One simple server-side approach is to remove all the options except the one that you want to be selected. Thus, in Zend Framework 1. Following on from Grant Wagners suggestion; here is a jQuery snippet that does it with handler functions instead of direct onXXX attributes:.

If you are using jquery validate, you can do the following below, I used the disabled attribute without a problem:.

Rather than the select itself, you could disable all of the options except for the currently selected option. This gives the appearance of a working drop-down, but only the option you want passed in is a valid selection. The style hides all the options and the groups when the select is in readonly state, so the user can not change his selection. What I found works great, with plain javascript ie: If the select dropdown is read-only since birth and does not need to change at all, perhaps you should use another control instead?

I was able to get this to work to create a readonly select options control using JavaScript.

I had to hide the select box and create a new hidden field. I've placed the code on my website for anyone to use as a reference. I managed it by hiding the select box and showing a span in its place with only informational value. On the event of disabling the.

is it possible to make