HTML5 details / summary
Let the browser do things for you by Tobias Krogh
Very often while reading the web you’ll come across blog posts and specs that mention all these new JavaScript APIs and elements offered by HTML5. Having said that, such elements are still used rarely due to the fact that specs tend to change or browser vendors only partly implement them.
Graceful degradation is the keyword. By using JavaScript we can offer a pretty similar experience. The first thing we note is that the details / summary functionality only work in Chrome at the moment so all other browsers will fall back to a simple solution.
General markup
<details> <summary>Topic</summary> <div>Detail content.</div> </details>
In this state the user won’t see the detail content. As soon as the summary is clicked the details element gets the attribute “open” (and could therefore be styled in another way).
<details open> <summary>Topic</summary> <div>Detail content.</div> </details>
The content in the div is now visible. At this point you may ask yourself why this only works in Chrome and not in other browsers?
Make it working in multiple browsers
I extended the markup pretty easily by adding an anchor and initially hiding the div content:
<details> <summary> <a class="toggle">Topic</a> </summary> <div style="display:none">Detail content.</div> </details>
If a browser now accesses this page we perform a simple check to see whether the element is supported (this script block is only representational. On the platform the JS is LazyLoaded in a dedicated file):
var testElement = document.createElement("details"); // check if object supports open property if ("open" in testElement) { // do mostly nothing } else { // fallback }
Note: This check returns “true” in Chrome 10 although it is not working. (The current stable version is 15, so I suppose we don’t need to deal with that.)
If the element is supported, the anchors get replaced with their text content and are therefore no longer trigger page reloads which helps to save an additional click observer that would prevent the default action. We also call “.show()” on all the div elements as they are hidden by the browser anyway.
On the other hand, if the element is not supported we observe a click event via delegation on the anchors and toggle the div element for the corresponding anchor when it is clicked.
Implementation
This is not just a theoretical article. I actually implemented this into XING’s help center, so if you now visit http://www.xing.com/help/ and then go to the FAQ section you will end up on a site where such a mechanism is used to display the sub-content of list entries. I doubt anyone will notice a difference between both variants.
Conclusion
Although we have to deal with older browsers down to Internet Explorer 6, this should not prevent anyone from trying out new things. HTML5 offers cool new possibilities to write and interact with the DOM. And most often it is possible with a little bit of investigational work that, in the end, delivers a degraded experience that still feels right.

Tobias Krogh works for XING as an enthusiastic Frontend Engineer. JavaScript is his language besides using semantic markup.
Good stuff, I wrote a bit about this too (http://www.iandevlin.com/blog/2011/12/html5/html5-the-details-and-summary-elements) which includes a cross-browser version (works on IE too – as it uses jQuery).
One thing that I would like to point out is that your test for details support is quite basic, and not bullet-proof. Mathias Bynens has posted a more robust solution, although it requires jQuery: http://mathiasbynens.be/notes/html5-details-jquery.