What the heck is an optgroup?
Posted At : September 9, 2008 7:25 AM | Posted By : Bob Silverberg
Related Categories: cfUniForm, ColdFusion
Although I've been developing web applications with ColdFusion for about a decade, I only discovered the existence of this html tag a few days ago, and it's way cool.
I've been reading up on creating accessible forms, and this article made the following suggestion:
Complex selects, with more than 11 options, should be coded with option groups and labels.
And contained the following example:
Pretty cool, eh? Here's the code:
2 <select id="favcity" name="favcity">
3 <optgroup label="Europe">
4 <option value="1">Amsterdam</option>
5 <option value="3">Interlaken</option>
6 <option value="4">Moscow</option>
7 <option value="5">Dresden</option>
8 </optgroup>
9 <optgroup label="North America">
10 <option value="2">New York</option>
11 <option value="6">Salt Lake City</option>
12 <option value="7">Montreal</option>
13 <option value="13">Toronto</option>
14 </optgroup>
15 <optgroup label="South America">
16 <option value="8">Buenos Aires</option>
17 <option value="9">Asuncion</option>
18 </optgroup>
19 <optgroup label="Asia">
20 <option value="10">Hong Kong</option>
21 <option value="11">Tokyo</option>
22 <option value="12">New Dehli</option>
23 </optgroup>
24 </select>
25</label>
I also decided to try out the cfUniForms Library by Matt Quackenbush to help me create more accessible forms and found it to be an extraordinarily well written and useful tool, with one exception, no optgroup support. So I spoke to Matt and decided to add that support myself. I also mentioned a couple of other enhancements that I'd like to see and he implemented them pretty much immediately. He'll be releasing a new version of the library with all of the new enhancements pretty much as soon as I finish writing this post ;-)
Update: Matt has released a new version of cfUniForm with these enhancements.
So, if you're interested in creating accessible forms and aren't already using a custom tag library to both reduce the amount of code you have to write and to encapsulate your field rendering logic, you may want to check out cfUniForm. And if you're not already using option groups, maybe you'll give them a try as well.
Update: Paul Marcotte has chimed in with his article about styling option groups.
Example:
A
Anders, Joe
Ayre, Jane
B
Bloke, Joe
...and so on. makes finding an entry in an alphabetized list MUCH easier for the user.
@Brad: Definitely there are times when you'll want to use an option group, and times when you won't. I don't think the article was trying to suggest that you should always use them when there are more than 11 options, but just that you should consider it.
@James: Definitely a great example of a use case for option groups! Thanks.
@Rob: That is very cool. Hmm, I wonder if I can get jQuery to do something like that?