Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable one element in a drop down list

Status
Not open for further replies.

vituja

Programmer
Oct 1, 2003
30
US
Hi all,

Is there a way to disable just one item in a dropdown list? I am able to change the color of an element with the code below using style sheets but I've been searching unsucessfully to find how to disable an element. Any help would be appricated. Thanks.

<style>option.Odd {color:red;}</style>

select size=&quot;1&quot; name=&quot;HRC&quot;>
<option value=&quot;CAS&quot;>A</option>
<option value=&quot;INH&quot;>B</option>
<option value=&quot;DNH&quot;>C</option>
<option value=&quot;BUL&quot;>D</option>
<option value=&quot;RUL&quot;>E</option>
<option value=&quot;TER&quot; class=&quot;Odd&quot;>F</option>
</select>
 
You could write a javascript that doesn't allow ppl to pick it - I'm just wondering why you'd have it there if not to be chosen?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
After a user saves an option to a db, a specific element in that list is no longer available for selection. I could just omit it from the list but it is better to show the user that that option did exist at one time.

 
Then write a script to prvent it's choice...

<script>
function badChoice(){
theSelect = document.getElementById(&quot;mySelect&quot;)
if (theSelect.options[theSelect.selectedIndex].value == -1){
alert(&quot;invalid selection&quot;)
theSelect.selectedIndex = -1
}
}
</script>

<select onBlur=&quot;badChoice()&quot; id=&quot;mySelect&quot;>
<option value=&quot;&quot;>Pick One
<option value=1>One
<option value=2>Two
<option value=-1>Three - Not Valid
</select>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Well, I'll be damned, I didn't know that but I just checked w3schools to see:
Code:
<select>
<option value=&quot;0&quot;>Pick Me</option>
<option value=&quot;1&quot;>Or me...</option>
<option value=&quot;2&quot; disabled>But not me</option>
<option value=&quot;3&quot;>Me you can though</option>
</select>
Hope it helps.
 
w3schools may want that in a compliant browser - but it didn't work in my IE5.5 - sorry

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
I admit, I only tested in Mozilla and saw that it does not work in the stupid IE. I have read the following on w3.org:

The following elements support the disabled attribute: BUTTON, INPUT, OPTGROUP, OPTION, SELECT, and TEXTAREA.

more at:
So it looks like another of those things where IE blatantly ignores the standards and users have to pay for it.
 
well &quot;stupid IE&quot; is what the vast majority (90%???) of ppl use..

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top