Home > Dojo > jQuery show/hide/toggle syntax in dojo

jQuery show/hide/toggle syntax in dojo

August 22nd, 2008

Ok, so yesterday I spent a good deal of the day trying to figure out how to manage the display of DOM elements. I searched Google, the dojo forums, the tests on archive.dojotoolkit.org with no simple answer. Coming from a heavy jQuery background I just could not believe that you had to bring in additional classes and/or write your own functions.

But dojo is a powerful beast, sometimes referred to as an elephant, so I guess I just have to “use the force…er um…power”

Desired Functions

dojo.query(’#hide’).hide();

dojo.query(.’show’).show();

dojo.query(’.toggle’).toggle();

WARNING: The code below is incredibly crude, it does not take into account for inline elements, there are no advanced arguments like fade or animation, and the toggle method is not fully implemented.

   1:  dojo.extend(dojo.NodeList, {
   2:      show: function(){
   3:          console.log(this);
   4:          this.forEach( item.style.display = 'block' );
   5:          return this;
   6:      },
   7:      hide: function(){
   8:          this.forEach(function(item){
   9:              console.log(item);
  10:              item.style.display = 'none';
  11:          });
  12:          return this;
  13:      },
  14:      toggle: function(){
  15:          this.forEach(function(item){
  16:              if(item.style.display == 'none'){
  17:                  //this.show();
  18:                  item.style.display = 'block';
  19:              } else {
  20:                  item.style.display = 'none';
  21:              }
  22:          });
  23:          return this;
  24:      }
  25:  });

 

PS: thanks Jayant for giving me the put to use the true power of dojo.

Share and Enjoy:
  • Digg
  • Facebook
  • del.icio.us
  • Print this article!
  • E-mail this story to a friend!
  • Google
  • description
  • description
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
  • Technorati
  • Tumblr

Dojo

  1. Nitin
    September 10th, 2008 at 05:15 | #1

    Please can you Post an Example for the above code snippet. Appreciate It. Thanks

  2. September 10th, 2008 at 10:15 | #2

    @Nitin -

    Put the code at the start of your JS file, after your dojo.require statements.

    Then you can hide dom elements using:

    dojo.query(’#id’).hide();

    To hide a bunch of items…I think…you can use:

    dojo.query(’.ClassName’).hide();

    That should hide everything with the class ClassName….

  3. November 6th, 2008 at 18:28 | #3

    Ironically, I just re-implemented these, with fade/speed args. You might enjoy it:

    http://higginsforpresident.net/2008/10/dojoshow-hide-toggle-and-more/

    Then I added some more:
    http://higginsforpresident.net/2008/11/dojo-the-missing-apis/

    all the code is available on google code: plugd

  4. November 6th, 2008 at 18:51 | #4

    Yeah! I new you would be a great dojo project lead! jk. Thats great, and I love that the plugd project it currently such a small add-on…

    btw…whats up with extending nodelist with xhr….I’m not sure what your goals are but it scares and excites me…some crazy (good) stuff could be done with that…

    http://code.google.com/p/plugd/source/browse/trunk/ajax.js

  1. No trackbacks yet.