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
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

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

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

Incredible Demos

  • By
    Create a Simple Slideshow Using MooTools

    One excellent way to add dynamism to any website is to implement a slideshow featuring images or sliding content. Of course there are numerous slideshow plugins available but many of them can be overkill if you want to do simple slideshow without controls or events.

  • By
    9 Incredible CodePen Demos

    CodePen is a treasure trove of incredible demos harnessing the power of client side languages.   The client side is always limited by what browsers provide us but the creativity and cleverness of developers always pushes the boundaries of what we think the front end can do.  Thanks to CSS...


  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!