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 "http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css";
</style>
<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="https://davidwalsh.name/demo/accordion/let-it-be.jpg" class="image" />
		<p>The Beatles were a pop and rock group from Liverpool, England...</p>
	</div>
	
	<-- Accordion item -->
	<div class="element" title="Rod Stewart" selected="true" dojoType="dijit.layout.ContentPane">
		<img src="https://davidwalsh.name/demo/accordion/every-picture.jpg" class="image" />
		<p>Roderick "Rod" David Stewart, CBE (born January 10, 1945), is a singer and songwriter...</p>
	</div>
	
	<-- Accordion item -->
	<div class="element" title="Oasis" dojoType="dijit.layout.ContentPane">
		<img src="https://davidwalsh.name/demo/accordion/oasis.jpg" class="image" />
		<p>Oasis are an English rock band that formed in Manchester in 1991...</p>
	</div>
	
</div>

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="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js" 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 */
	dojo.require('dijit.layout.AccordionContainer');
	dojo.require('dijit.layout.ContentPane');
	/* when all classes have loaded... */
	dojo.ready(function() {
		/*
			don't need to do anything programmatically!
			parseOnLoad and dojoType does the magic!
		*/
	});
</script>

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
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

Incredible Demos

  • By
    Scrolling &#8220;Agree to Terms&#8221; Component with MooTools ScrollSpy

    Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...

  • By
    dwImageProtector Plugin for jQuery

    I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers...

Discussion

  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
    accordion.startup();
    
    • Updated the CSS files — thanks Pete!

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

    • Gerard

      Hi,

      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:

      http://pastie.org/2400915

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

      http://pastie.org/2400917

  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: http://blogriot.com/life-list/

    • 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
    Ty…

  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!