Create a Simple Dojo Accordion

By  on  

Let's be honest:  even though we all giggle about how cheap of a thrill JavaScript accordions have become on the web, they remain an effective, useful widget.  Lots of content, small amount of space.  Dojo's Dijit library provides an incredibly simply method by which you can add accordions to your website.  Let's take a look at how we can implement a Dojo accordion!

Dojo Accordion

The Basic Dijit HTML

<style type="text/css">
	/* bring in the claro theme */
	@import "";
<body class="claro">

The first piece of HTML requires grabbing the required CSS theme files and adding the theme name as a class to the BODY tag.  All widgets on the page will use the claro theme unless otherwise specified.  Once we have those details out of the way, we can code the HTML structure of Accordion:

<-- The container must be a dijit.layout.AccordionContainer -->
<div id="accordion" dojoType="dijit.layout.AccordionContainer">

	<-- Accordion item -->
	<div class="element" title="The Beatles" dojoType="dijit.layout.ContentPane">
		<img src="" class="image" />
		<p>The Beatles were a pop and rock group from Liverpool, England...</p>
	<-- Accordion item -->
	<div class="element" title="Rod Stewart" selected="true" dojoType="dijit.layout.ContentPane">
		<img src="" class="image" />
		<p>Roderick "Rod" David Stewart, CBE (born January 10, 1945), is a singer and songwriter...</p>
	<-- Accordion item -->
	<div class="element" title="Oasis" dojoType="dijit.layout.ContentPane">
		<img src="" class="image" />
		<p>Oasis are an English rock band that formed in Manchester in 1991...</p>

The title attribute text will become the Accordion control's heading.  Adding a selected attribute will make the given accordion pane start as the selected pane.

Including Dojo with parseOnLoad; Requiring Dijit Widget Classes

<script src="" type="text/javascript" djConfig="parseOnLoad:true"></script>

We start by pulling Dojo into the page with a normal SCRIPT tag (AOL CDN, Google CDN, local build).  Note that I've added a djConfig attribute with parseOnLoad:true; -- this will instruct Dojo to scour the page looking for Dijit widgets once the required classes have loaded.  How do we require the proper classes?  By using dojo.require, of course:

<script type="text/javascript">
	/* require necessary classes */
	/* when all classes have loaded... */
	dojo.ready(function() {
			don't need to do anything programmatically!
			parseOnLoad and dojoType does the magic!

With the proper classes loaded, Dojo will now find all widgets and inject widgets into the page as necessary.

All JavaScript toolkits have provided an easy way to create accordions and Dojo/Dijit is no exception!

Recent Features

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Event Delegation with MooTools

    Events play a huge role in JavaScript. I can't name one website I've created in the past two years that hasn't used JavaScript event handling on some level. Ask yourself: how often do I inject elements into the DOM and not add an...

  • By
    Upload Photos to Flickr with PHP

    I have a bit of an obsession with uploading photos to different services thanks to Instagram. Instagram's iPhone app allows me to take photos and quickly filter them; once photo tinkering is complete, I can upload the photo to Instagram, Twitter, Facebook, and...


  1. The CSS imports are redundant. By including claro.css from the CDN you are already including the two other files you referenced. The addition http hit is unnecessary.

    I would have preferred to see a programmatic version that did not use the dojoType. The API is very simple:

    var accordion = new dijit.layout.AccordionContainer({ ... }).placeAt("someNode");
    new dijit.layout.ContentPane({ ... }).placeAt(accordion);
    // repeat
    • Updated the CSS files — thanks Pete!

    • The declarative route is probably the easier route to explain which is why I went with it.

    • Gerard


      How _do you do it the Programmatic way? Not by loading the content via dojo/javascript but manipulating the html itself. i.e. when you turn off javascript the content is still there.

      Also, what are the options on the accordion, there is absolutely Nothing in the dojo reference guide or API, for example the titles have a +/- symbol on them, obviously you can turn this off?

      The docs are dreadful and everything is done in the declarative.

    • The programmatic way is provided by Pete Higgins above.

    • Gerard

      The code I posted above was foobar-ed, the seemed to be stripped completley, i’ll use pasties:

      I’m looking for the pragmatic way using the following html:

      Alternatively we can sprinkle in some classes (should be able to do it without id’s) and shouldn’t require dojo specific class names:

  2. pavs

    I really like this.

    Is there any reason why the demo has a “jerky” effect the first time it loads? It seems to load the whole content and slides back up (Chrome Dev 6.0495).

    I did something similar (I know mine’s ugly :) but it does what I need for now), do think you can do something similar in dojo (for one of your future blog post) , without opening any slide by default?

    Here is mine:

    • Part of it is the Dijit creation process from an existing set of HTML blocks. Using the programmatic method of widget creation or adding an overlay may help.

  3. MarcOs

    is possible to change the height of the accordion ^^ ?
    Im trying to put a flash animation on the accordion but is higher

  4. Really a cool tutorial, thank you David.

  5. How can I disable (or replace) the animation for a dojo accordion?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!