How it all started...
Yes, I admit, I am one of those post-modern programmers who search the Web for solutions and code. Time to refund something.
One task I often come across at work is to find workarounds for Internet Explorer HTML, CSS, or JavaScript - um - deficiencies. As you can imagine, this task can consume lots of time and would often not be possible without post-modern programming techniques.
Today's topic is IE's lack of supporting the disabled attribute for option elements. It simply ignores the attribute: a disabled option is still selectable. This is not even fixed in IE7.
Several solutions can be found on the Web, which fall mainly into two categories:
- Complicated JavaScript solutions that try to change the selection afterwards when detecting that the selected item is actually disabled (e.g. by Alistair Lattimore and by Kaleb Walton).
- The idea to simply replace a disabled option by an
optgroup, as described by Ben.
I like the second approach much better, not because it refrains from using JavaScript, but because the behavior in the browser is as intended and it does not interfere with other event handlers. But what I do not like about the second solution is that one has to care about the IE bug during rendering the HTML code.
For the first solution, some posts suggested to use DHTML Behaviors. Although this is an IE-prorietary feature, it is really okay to use it here, namely as a workaround for an IE-specific problem.
So my idea was to combine the HTML of the second approach with the "JavaScript will take care of it" attitude of the first approach and came up with the following solution which did not seem to be available on the Web before. (In fact, this idea became the missing push to eventually get me blogging.)
Fables of the Reconstruction
To cite from Ben's solution:
The key is to replace your disabled
options with anoptgroup. Why does this work?
optgroups are alreadydisabled— you can’t select them, so you get immediate feedback.optgroups don’t look disabled, but you can use CSS to change the look.
As I said, the only real drawback of Ben's solution is, as he mentions, that you have to care about constructing the IE-specific HTML yourself. So why not have JavaScript do the reconstruction inside the browser? All the JavaScript has to do is scan all option elements for the disabled attribute and replace them by optgroups. Matters become a bit more complicated when disabled options inside optgroups are also supposed to work, but we'll also cover that use case.
For the following code fragment, let's assume there is a variable element refering to an option element. We have to check its disabled property and, to make sure we produce no nested optgroups, the parent node not to be an optgroup. Then, we retrieve the label of the option element, which is its innerHTML, and replace it by a newly constructed optgroup with that label and the style suggested by Ben:
if (element.disabled && element.parentNode.nodeName!="OPTGROUP") {
var label = element.innerHTML;
var optgroup = document.createElement("<optgroup label='"
+ label
+ "' style='color:graytext; font-style:normal;'/>");
element.parentNode.replaceChild(optgroup,element);
}
I could have used a simple external JavaScript file that adds an onload event handler and scans the whole document for option elements. Instead, I chose to use DHTML Behaviors, which leads to an elegant and concise solution and is "allowed" in this case for the reasons explained above.
For problems like this one, where you just want to execute some JavaScript code on all elements that match some CSS rule, DHTML Behaviors are really easy to handle. Just define an IE-specific CSS rule like this:
option { behavior: url("disabled-option.htc"); }
Because this rule is IE-specific and standard-compliant browsers may complain about it (at least Firefox issues a warning in its console), it should go into an IE conditional comment:
<!--[if IE]>
<style>
option { behavior: url("disabled-option.htc"); }
</style>
<![endif]-->
An HTC file is essentially a JavaScript source code file wrapped with some meta information. In this case, the meta information is trivial, and the current element is handed in as a parameter named element (what coincidence and surprise!), so that we just have to wrap the JavaScript code fragment given above with
<public:component>
<script>
...
</script>
</public:component>
and put it into a file called "disabled-option.htc".
Group Pressure
Ben points out that since you cannot nest optgroup elements, it requires a bit more tricks to find a workaround for disabled options inside an option group. He suggests the following:
- Use non-breaking spaces to indent the menu items as a way to emulate a nested optgroup.
- Use other empty
optgroupitems (with no extra CSS formatting) to serve as the group headings.
That means we need another Behavior for optgroups that checks whether the group contains a disabled option, and if so, moves all options to the top level, adding non-breakable spaces to emulate indention, and converts disabled options to optgroups on the fly. Let's call this Behavior "optgroup-with-disabled-options.htc". If you need this feature, add a corresponding CSS rule to the style that is only visible for IE (see above):
optgroup { behavior: url("optgroup-with-disabled-options.htc"); }
The contents of "optgroup-with-disabled-options.htc" looks like this:
<public:component>
<script>
// check whether it contains a disabled option:
for (var i=0; i<element.childNodes.length; ++i) {
if (element.childNodes[i].nodeName=="OPTION" &&
element.childNodes[i].disabled) {
// move all options one level up, adding non-breakable
// spaces to indent and converting disabled options:
var parent = element.parentNode;
var insertionPoint = element.nextSibling;
for (var i=element.childNodes.length-1; i>=0; --i) {
var option = element.childNodes[i];
if (option.nodeName=="OPTION") {
var label = option.innerHTML;
if (option.disabled) {
element.removeChild(option);
option = document.createElement("<optgroup label=' "
+label
+"' style='color:graytext; font-style:normal;'/>");
} else {
option.firstChild.data = " "+label;
}
insertionPoint = parent.insertBefore(option,insertionPoint);
}
}
break;
}
}
</script>
</public:component>
Note that to emulate IE's standard indention depth, you need six spaces for normal options, but only four spaces for option groups that simulate disabled options (probably because the option group is bold?).
Conclusion
Putting it all together, you can now use the disabled attribute on option elements, and the following happens:
- All non-IE browsers take the conditional comment as a comment and do nothing special. Disabled options work, anyway.
- IE uses the style inside the conditional comment and thus defines the given Behaviors for all
optionandoptgroupelements.- For each
optionelement, the JavaScript code defined in "disabled-option.htc" is executed and replaces disabled options by optgroups. - For each
optgroupelement, the JavaScript code defined in "optgroup-with-disabled-options.htc" is executed, moves alloptionsinside anoptgroupthat contains any disabled option to top level, indents them, and again replaces disabled options by optgroups.
- For each
The result looks like this:
The advantages of this new solution:
- You only have to include a few lines in the
headof an HTML page which are treated as comment by any validator and any standard-compliant browser. - Then, you can use the standard-compliant way for disabling options and it will work even in IE.
- All usual event handlers defined on the select element work as before.
- For the anti-JavaScript-lurkers: If JavaScript is turned off, the situation isn't any worse than before: simply, the IE bug is still present.
The only disadvantages I could think of:
- The DOM is manipulated at run-time. If for example another script is manipulating the options (quite a common use case), it may have to be adapted to be aware of the disabled-option Behavior.
- Maybe a small rendering performance impact if you have many many options on a page.
- IE ignores most CSS style on options. Luckily, the font
colorcan be changed tograytext, butfont-styleandfont-weightdo not seem to have any effect; the text is always bold and italic. I think this minor design deviation is bearable.
Please let me know if you found this solution useful!



6 comments
1. Chad (anonymous), Jun 30, 2008 8:12:17 PM #
Excellent post. Saved me a lot of time. Nice way to take an idea and make it that much better.
2. Schnies (anonymous), Jul 30, 2008 10:49:40 AM #
Many Thanks, work for me excellent.
Short Info: You have to declare the Variable 'element' not in the htc-File, but after the HTML-Form. For Example:
Heino
Michael Jackson
Tom Waits
Nina Hagen
Marianne Rosenberg
Hansi Rosenberg
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.Testformular;
}
else {
theform = document.forms["Testformular"];
}
var element = Testformular.Auswahl;
Best Wishes,
Schnies
3. Honti, Balázs (anonymous), Dec 8, 2008 4:34:12 PM #
Hello! Recursive way of disabling whole optgroups:
Modify optgroup-with-disabled-options.htc file as:
down=element.disabled;
...
if (element.childNodes[i].nodeName=="OPTION" && (down||element.childNodes[i].disabled))
...
if (down||option.disabled)...
Good luck,
Balázs
4. sxdoukou, Jul 16, 2009 7:03:58 AM #
People taking theghd stairs instead of elevator hair straightenersat work can expectghd uk to live longer, according ghd straightenersto a Swiss studyghd hair styler released on Monday.
Regularlyghd hair straighteners walking from floor toed hardy floor in an office buildinged hardy clothing decreased mortalityed hardy caps risk by 15 percent,ed hardy discounts said Dr. Philippe Meyer,ed hardy online the main author of theed hardy tshirts study, which was done ed hardy womenat the University HospitalCheap GHD of Geneva.
Banning the use of liftsGHD pure and escalators led to better fitness,GHD kiss less body fat, trimmer waistlines and a dropGHD MK4 Black in blood pressure, the study found.
Using the stairs GHD MK4 Pinkimproves fitness, body composition,GHD MK4 Gold blood pressure and lipid profiles, Meyer was quoted as GHD Purplesaying by the Swissinfo news website.
"The challenge remains to develop Tiffany Necklacessuccessful population-based interventions, which promote physical activitiesTiffany Ring that can be easily integrated Tiffany Bangleinto everyday life," he said.
For the study, 77 employees from GenevaDiscount Tiffany University with a sedentary lifestyle were recruited to take only the stairs over aTiffany 1837 three-month period, Swissinfo reported.
Results showed an increase Tiffany Atlasin aerobic capacity, a decline in waist circumference, Tiffany Watchweight, fat mass, blood pressure and cholesterol.
"This suggests that stair Tiffany Paloma Picassoclimbing can have major public health implications." Meyer said.
Professor Adam Timmis,Tiffany Brooch consultant cardiologist at The London NHS Trust, said: "It's a small study but valuableTiffany Cuff Links because it provides a practical way for busy working people to increase Tiffany Key Ringtheir exercise capacity.
"Although the amount of exercise appears small, the benefits were clear in improving physical fitness and reducing body fat and blood pressure."
5. sxdoukou, Jul 16, 2009 7:06:04 AM #
Feeling stressed orTiffany Bracelet anxious at an inability Tiffany Earringsto access the Internet? Don't worry, Tiffany ringsyou're not alone and now there'sTiffany jewellers a word for it: "discomgoogolation."
NearlyTiffany jewelry half of Britons -- 44 percentTiffany jewellery -- are discomgoogolation sufferers,Tiffany silver according to a survey, with over ghda quarter -- 27 percent -- admittingGHD Dark to rising stress levels when they ghd straightenersarghd hair straightenerse unable to go online.
"The proliferationghd hair styler of broadband has meant Cheap GHDfor the first time inGHD pure history we've entered a cultureGHD kiss of 'instant answers,"' GHD MK4 Blacksaid psychologist Dr David GHD MK4 PinkLewis, who identifiedGHD MK4 Gold discomgoogolation by measuring GHD Purpleheart rates and brainwavedon ed hardy activity.
The term comesed hardy shoes from "discombobulate," whiched hardy apparel means to confuse or frustrate, ed hardy menand Google.
"A galaxy of informationed hardy women's is just a mouse click awayed hardy bag and we have become addicteded hardy tshirt to the web," added Lewis. "Whenugg boots unable to get online, discomgoogolation takes over.
"It was surprising to uggssee the stress this led to brain activitywholesale ugg boots and blood pressure inugg sheepskin boots participants both increase in response to being cut off fromchina wholesale the Internet."
The survey also wholesale digital camerasfound 76 percent of Britons could not live withoutmp4 watches the Internet, with overWholesale Mp4 half of the population usingwholesale t shirts the web between one and four hours a dayWholesale handbags and 19 percent of people spending wholesale clothingmore time online than with their family in a week.
Forty-seven percent oWholesale jewelry those polled believed the Internet was wholesalemore important in people's lives than religion, with one in five people paying the InternetWholesale Jewelry more attention than their partner.
Commissioned by Wholesale fashion jewelryinformation service 118118, the YouGov pollWholesale costume jewelry questioned 2,100 Britons during the first week of July.
6. sxdoukou, Jul 16, 2009 7:06:18 AM #
The first day ofnokia 8800 school our professorNokia 8800 Sirocco introduced himself and Watcheschallenged us to get to Cheap Watchesknow someone we didn’Cartier Watchest already know.I stoodReplica watches up to look around when a gentlecheap watches hand touched my shoulder.I Replica Omega Watchesturned around to find a wrinkledIWC Watches,little old lady beamingReplica Breitling up at1) me with a smile that lit upChanel Watches her entire being.She said,“Hi franck muller watcheshandsome.My name is Rose.I‘ m eighty-seven yearsreplica watches old.Can I give you a hug?Gucci Replica”I laughed and enthusiastically2) Cartier Watchresponded,“Of course you may.”andreplica watches she gave me a giant squeeze.
“Why are you inceramic replica watches college at such a young,swiss replica watchesinnocent age?”I asked.She replica burberry watchesjokingly replied,“I’m here to meet data processing servicea rich husband,get married,have a couple of children,data processingand then retire and travel.”“No,data processseriously?”I asked.I was curious Ceramic tilewhat may have motivated her to be taking on Micro sd cardsthis challenge at her age.
“I always dreamed Wholesale jewelryof having a college educationWholesale clothing and now I‘m getting one.ugg boots”she told me.After class we walked to thewholesale ugg boots student union building andugg boots shared a chocolate milk uggsshake3).We became High pressure blowerinstant friends.Every day for the next three months weCommercial blower would leave class together and talk wholesalenonstop.I was always china wholesalemesmerized listening to this“time machine”as she sharedwholesale electronicsher wisdom and experience with me.
Overmp4 watches the course of the year,Rose became aWholesale Mp4 campus icon and she wholesale beadseasily made friends wherever she went.wholesale digital cameraShe loved to dress up and she reveled4) in thewholesale camcorders attention bestowed5)mp3 watch up her from the othermp5 player students.She was living it up6) .At the end of the semesterwholesale pda mobile phones we invited Rose to speakpda mobile phones at our football banquet.I‘ll never forget what she taughtMichelle Obama's Shoes us.She was introduced and luxurious cell phonesstepped up to the podium.wholesale brand cell phonesAs she began to deliver her prepared wholesale furniturespeech,she dropped her three by five cards on the floor.Furniture WholesaleFrustrated and a little embarrassed she leaned into the microphone and simply said,“I’m sorry I‘m so jittery7).I gave up beer for Lent and this whisky is killing me.