views:

888

answers:

5

I have a multiple selection SELECT field which I don't want the end user to be able to change the value of.

For UI reasons, I would like to be able to do this without using the disabled="true" attribute. I've tried using onmousedown, onfocus, onclick and setting each to blur or return false but with no success.

Can this be done or am I trying to do the impossible?

+1  A: 

Could you do it with an onchange event?

<select onfocus="this.oldIndex=this.selectedIndex" onchange="this.selectedIndex=this.oldIndex">
ceejayoz
+2  A: 
Jack M.
+4  A: 

What are your UI reasons for not disabling the select?

If a control can't be modified, it needs to take on a disabled look otherwise you're going to really confuse your users.

17 of 26
+3  A: 

I know you mentioned that you don't want to, but I actually think that using the disabled attribute is a better solution:

<select multiple="multiple">
    <option value="volvo" selected="true" disabled="disabled">Volvo</option>
    <option value="saab" disabled="disabled">Saab</option>
    <option value="opel" disabled="disabled">Opel</option>
    <option value="audi" disabled="disabled">Audi</option>
</select>

If necessary, you can always give the select a class and style it with CSS. This solution will work in all browsers regardless of scripting capabilities.

travis
Hmm, doesn't seem to work in IE7. I can still select them. In Firefox they are disabled.
Frank Schwieterman
This solution won't work on IE6, IE7
Ovesh
Damn IE. I wonder if there's an MS-specific equivalent to disabled="disabled"? Maybe readonly="readonly"?
travis
No, there is none. They implemented the HTML4 disabled attribute in ie8 rc2 FIRST TIME
naugtur
A: 

@Jack & @17 of 26, good point but the end user will be expecting the select box to be disabled so that confusion shouldn't be an issue.

I should have been clearer about why I couldn't just disable the control.

The application that will be using this will need to disable the selection of the options and there is a requirement that the "locked" control still maintain the look and feel of normal form controls.

Jared