views:

4372

answers:

7

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.

The only problem is that disabled HTML form inputs don't get included in the POST / GET data.

What's the best way to emulate the readonly attribute for a select tag, and still get the POST data?

+2  A: 

You can re-enable the select object on submit.

Kevin
+11  A: 

How about adding a hidden input with the same value?

If you reenable your SELECT, you should copy it's value to the hidden input in an onchange event.

Max
Thats what I did, thanx :)
Jrgns
Exactly, this is the standard practice as far as I know.
BobbyShaftoe
Agreed. This method is noscript safe as well.
annakata
A: 

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.

Adam Bellaire
In theory a great idea - but there is NO support for disabled options in IE before IE8. http://tinyurl.com/yle4bto
scunliffe
+4  A: 
<select id="countries" onfocus="this.defaultIndex=this.selectedIndex;" onchange="this.selectedIndex=this.defaultIndex;">
<option value="1">Country1</option>
<option value="2">Country2</option>
<option value="3">Country3</option>
<option value="4">Country4</option>
<option value="5">Country5</option>
<option value="6">Country6</option>
<option value="7" selected="selected">Country7</option>
<option value="8">Country8</option>
<option value="9">Country9</option>
</select>

Tested and working in IE 6, 7 & 8b2, Firefox 2 & 3, Opera 9.62, Safari 3.2.1 for Windows and Google Chrome.

Grant Wagner
A: 

If the select dropdown is read-only since birth and does not need to change at all, perhaps you should use another control instead? Like a simple <div> (plus hidden form field) or an <input type="text">?

Added: If the dropdown is not read-only all the time and JavaScript is used to enable/disable it, then this is still a solution - just modify the DOM on-the-fly.

Vilx-
It's not readonly from the beginning. I use JavaScript to change and update. If a previous dropdown has a certain value, this one becomes readonly.
Jrgns
Then perhaps you can replace this dropdown with a textbox on-the-fly?
Vilx-
Yup, but the always there hidden input is more elegant in my opinion
Jrgns
A: 

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. HTML Readonly Select: http://www.codepug.com/readonlySelect.html

Cheers, --X .

+1  A: 

Following on from Grant Wagners suggestion; here is a jQuery snippet that does it with handler functions instead of direct onXXX attributes:

var readonlySelect = function(selector, makeReadonly) {

    $(selector).filter("select").each(function(i){
        var select = $(this);

        //remove any existing readonly handler
        if(this.readonlyFn) select.unbind("change", this.readonlyFn);
        if(this.readonlyIndex) this.readonlyIndex = null;

        if(makeReadonly) {
            this.readonlyIndex = this.selectedIndex;
            this.readonlyFn = function(){
                this.selectedIndex = this.readonlyIndex;
            };
            select.bind("change", this.readonlyFn);
        }
    });

};
thetoolman