Coder’s Eye

A site about one of the three passions in my life.

Coder’s Eye header image 2

Using Dojo for a simple show/hide toggle effect.

November 25th, 2006 · 4 Comments

DojoWithout a doubt, my favorite Javascript library is Dojo Toolkit. Unfortunately, it can be a bit challenging to learn via the dojo.manual, especially if you aren’t interested in the widgets, just the DOM, UI, and FX libraries.

I just wrapped up a project where I used Dojo quite heavily on the site. One effect I thought was particularly effective was a simple show/hide toggle with nice fades. Take a look at the live page.

It is a simple page, and the code that produces is it is simple, easily understood, and quite cross-browser friendly. Note particularly that I did not use any inline javascript.

How I implemented the effect

Markup

First, I made a div section which would be toggled, giving it the id of “suggestions”. Also, I made a graphical button and gave it the id “hider”. Lastly, I made another matching button with the “toggle back on” text.

Code

Then, I activated the toggle with this code:

dojo.require("dojo.lfx.*");

// show/hide toggle the "suggestions" div
function toggleSuggestions() {
   var suggestion = dojo.byId("suggestions");
   var hider = dojo.byId("hider");

   var display = suggestion.getAttribute('displayed');

   if (display == 'on') {
     dojo.lfx.toggle.fade.hide(suggestion, 1000);
     // change the button image
     hider.setAttribute("src","media/images/show-suggestion.gif");
     display = 'off';
   } else {
     dojo.lfx.toggle.fade.show(suggestion, 1000);
     // change the button image back
     hider.setAttribute("src","media/images/hide-suggestion.gif");
     display = 'on';
   }
   // remember the toggle state
   suggestion.setAttribute('displayed', display);
}

// dom hook and setup
function serenityInit() {
   var hider = dojo.byId('hider');
   // wire the event without using inline javascript, sweet!
   dojo.event.connect(hider,'onclick',toggleSuggestions);
   var suggestion = dojo.byId("suggestions");

   // set the default displayed state
   suggestion.setAttribute('displayed', 'on');
}

// Add the init to the load chain for the page.
dojo.addOnLoad(serenityInit);


Technorati Tags: , , ,


Tags: Javascript

Bookmark this article

del.icio.us:Using Dojo for a simple show/hide toggle effect. digg:Using Dojo for a simple show/hide toggle effect. spurl:Using Dojo for a simple show/hide toggle effect. wists:Using Dojo for a simple show/hide toggle effect. simpy:Using Dojo for a simple show/hide toggle effect. newsvine:Using Dojo for a simple show/hide toggle effect. blinklist:Using Dojo for a simple show/hide toggle effect. furl:Using Dojo for a simple show/hide toggle effect. reddit:Using Dojo for a simple show/hide toggle effect. fark:Using Dojo for a simple show/hide toggle effect. blogmarks:Using Dojo for a simple show/hide toggle effect. Y!:Using Dojo for a simple show/hide toggle effect. smarking:Using Dojo for a simple show/hide toggle effect. magnolia:Using Dojo for a simple show/hide toggle effect. segnalo:Using Dojo for a simple show/hide toggle effect. gifttagging:Using Dojo for a simple show/hide toggle effect.

4 responses so far ↓

  • 1 alepuzio // Dec 22, 2006 at 5:16 am

    Hello,
    me too I use DojoToolkit, but I think that this library is better for big project. Fro small project for me it’s better ScriptACulo or Prototype.
    What do you think?

    Hi

  • 2 Bruce // Dec 22, 2006 at 7:42 am

    Alepuzio, I agree with you. If I hadn\’t already been using Dojo, I wouldn\’t recommend it for such a simple effect. Actually, my favorite lightweight Javascript library is jQuery.

    In that library, you could duplicate the functionality like so:

    
    $(document).ready(function() {
        $('hider').attr('show',true);
        $('hider').click(function() {
            var state = !($('suggestions').attr('displayed'));
            var src = \"media/images/\"
                          + (state ? \"show\" : \"hide\")
                          + \"-suggestion.gif\";
    
            $('suggestions').toggleSlide(\"slow\")
                 .attr('suggestions', state);
            $('hider').attr('src', src);
    });
    
  • 3 Bruce // Dec 22, 2006 at 7:49 am

    Not sure what is up with the backslashes in my comment. Remove them to make the code work.

  • 4 Yann // Sep 26, 2008 at 9:08 am

    How i can take this with suggestions closed?
    where i need to change?
    I think need change ‘displayed’ but Im not sure, and don’t know what i need to replace by…

    so… sorry my english, Im from Brazil
    see you!

Leave a Comment