Slicker Show and Hide

read 248 comments

Last time I showed you how to make something appear and disappear on a web page. This time I'll show you how to do it with style.

Like we did last time, we'll start with our $(document).ready() and put everything else inside of it.

Adjust the Speed

This time, however, we're going to adjust the speed at which our item shows and hides. To do so, we put a speed indicator — "slow" or "normal" or "fast" or a number of milliseconds (1000 = 1 second) — inside the parentheses:

JavaScript:
  1. $('#slickbox').show('slow');

Note: If you use a speed word, put it inside quotation marks (either single or double); if you use a number, omit the quotation marks: $('#slickbox').show(500);

Attach Effects to Events

The final step here is to attach the effects to events. Last time we attached our effects to the "onclick" event of buttons. This time we'll attach them to links. But here's the rub: Links already have events attached to them. They take the user to whatever URL is indicated in the "href" attribute, usually another page. That means that we not only need to make something happen, but we also need to stop the default thing from happening. We can prevent the default behavior by adding return false;.

So let's take a look at how we would tie the "onclick" event of one link with an ID of "slick-show" to the "show" effect. Here, show() will display a DIV with an ID of "slickbox":

JavaScript:
  1. // shows the slickbox DIV on clicking the link with an ID of "slick-show"
  2.   $('a#slick-show').click(function() {
  3.     $('#slickbox').show('slow');
  4.     return false;
  5.   });

Notice that we attach ".click()" to "a#slick-show". In jQuery, events will often have anonymous, or lambda, functions inside them. That's why inside the parentheses of ".click()" you see function() { ... }. By the way, it's called an anonymous function because it has no name associated with it. I don't know why it's also called a lambda function. Can someone enlighten me?

Here is the final code for three effects — show(), hide(), and toggle(). I've added $('#slidebox').hide() as the first line after $(document).ready() to hide that DIV when the page loads.

JavaScript:
  1. $(document).ready(function() {
  2.  // hides the slickbox as soon as the DOM is ready
  3.  // (a little sooner than page load)
  4.   $('#slickbox').hide();
  5.  // shows the slickbox on clicking the noted link  
  6.   $('a#slick-show').click(function() {
  7.     $('#slickbox').show('slow');
  8.     return false;
  9.   });
  10.  // hides the slickbox on clicking the noted link  
  11.   $('a#slick-hide').click(function() {
  12.     $('#slickbox').hide('fast');
  13.     return false;
  14.   });
  15.  
  16.  // toggles the slickbox on clicking the noted link  
  17.   $('a#slick-toggle').click(function() {
  18.     $('#slickbox').toggle(400);
  19.     return false;
  20.   });
  21. });

Look closely at the code and you'll see that our slickbox will show at a slow speed, hide at a fast speed, and toggle somewhere in between (in 400 milliseconds).

Demo 1

Try it out for yourself. Just click on the three following links:

Show the box  Hide the box  Toggle the box

This is the box that will be shown and hidden and toggled at your whim. :)

Newbie Tip

For those of you brand new to JavaScript as well as jQuery, the lines of code that begin with two slashes ( // ) are comment lines that have no effect on the code whatsoever. You can also create multi-line comments by enclosing them in /* and */

Other Effects

Other simple jQuery effects for showing or hiding elements include:

  • slideUp()
  • slideDown()
  • slideToggle()
  • fadeIn()
  • fadeOut()

Demo 2

Try the slide effects to see how they differ from show/hide:

Slide the box down  Slide the box up  Slide toggle the box

Bonus

For more sophisticated effects, check out Stefan Petre's Interface plugin jQuery UI

UPDATE

If this type of showing and hiding isn't exactly what you're looking for, please check out my other entries and the jQuery UI Accordion:

comment feed

248 comments

  1. Ali

    First of all I want to say thank you for such good tutorial... :)

    I have no problem with the code but I had a question if you could help me with... I have an anchor link that takes you to a section where I've applied this show/hide feature. What I want to do is when a user clicks on the anchor link it should take them to that section and open the closed div. Is there a way to do this? I would really appreciate if you/anyone could help me with this!

  2. Hello, thanks for the code. And sorry for my english.

    What if we try to hide/show dinamic content like in a large list?
    For example:
    - Item 1
    hidden content 1
    - Item 2
    hidden content 2
    .
    .
    - Item n
    hidden content n

  3. Jay

    Is there a way to show all hidden sections with just 1 link?

    In my code I have 2 table rows that have hidden content (see code).. what I want to do is create a link that can open hidden content in both rows. At the moment I have to click on individual links to open their hidden content. It's just a "Show All" content feature I'm trying to add to the site. Any help is appreciated!

    Test Link 1

    Hidden Content ...

    Test Link 2

    Hidden Content ...

    • It's quite simple.Instead of using id tag use class tag for both your elements.

      
      
      content number one
      content number two

      then instead of

      
      $('#slickbox').hide();
      

      use

      
      $('.tohide').hide();
      

      this way you can hide as many element as you wish

  4. rob hindley

    I am a designer trying to make this work.
    I have copied and pasted from your code but doesn't work.
    So I link to slick.js in the HEAD?
    And the jQuery library?
    Do I need to create my own css as well?

    • as far as I know hide() show() and toggle() just adjust div size from zero to you defined size
      let's say that you have div width 200px and height 100px
      it sipmly start thread for adjusting width and height from 0 to 200px
      try define width height and background color
      put display:none; if you want to use show()

    • LB

      Yeah, someone neglected to tell you to download jQuery.js. Ok, maybe it's obvious to many developers but if you're a designer looking for extra flare. You'd break your head trying to figure out why it's not working!

      Cheers and here is what you need to link in your
      Download jQuery NOW!

  5. your way of presenting tutorial is very poor man...i love your tutorial but i cannot implement...you should attach source files....hope you will do it next time...

    Thanks

  6. will

    hey karl! Great tutorial! I've been looking for a nice animation to slide in my divs and your's works perfectly!

    I've got 1 little question though:

    How can make several divs show/hide? The above code applies to 1 div, but what if I have multiple divs with different content and i want to show them seperately when someone clicks on a link? I've tried making multiple divs, but it either shows all the divs or doesnt work at all.

    Im not a webdevelopper, so sorry if my question is a bit stupid :)

    • Jimtiger

      Hello Karl
      Once again, nice work on the tutorial just what I was looking for. Apologies if my question is in the wrong neighbourhood:) I am trying to marry flash and jquery into browser bliss. My query...... I would like to trigger demo 1's 'toggle the box' from a button inside a swf, I am using AS2. I've discovered that external interface call would be suitable for achieving this. P.S - I am very new to jquery.
      Any help/ examples would be greatly appreciated. Thanks.

  7. Jimtiger

    Hello Karl
    Sorry for posting twice, I just realised my mistake.
    Once again, nice work on the tutorial just what I was looking for. Apologies if my question is in the wrong neighbourhood:) I am trying to marry flash and jquery into browser bliss. My query...... I would like to trigger demo 1's 'toggle the box' from a button inside a swf, I am using AS2. I've discovered that external interface call would be suitable for achieving this. P.S - I am very new to jquery.
    Any help/ examples would be greatly appreciated. Thanks.

    • Hi Jim. I was searching for a solution to my problem when I found your comment. I'm having the same issue as yours, except I need to fix this only on IE and Chrome. How would I trigger a jQuery click event (for a modal box) on a HTML anchor link with an animated flash movie inside? This works fine with FF and Opera, but not in IE and Chrome though. Anybody who can help? Thanks.

  8. Sojourner

    Hi! Great script... I'd just like to know what code I should add if I am having multiple contents going to be hidden and shown differently in one page, BUT I don't want to just copy and paste the .js code and rename it 1-2-3--n... you know what I mean?

    For example:

    Pseudocode
    [div class="content1"] Content 1 texts
    [a class="toggle content 1"] toggle 1

    [div class="content2"] Content 1 texts
    [a class="toggle content 2"] toggle 2

    [div class="content3"] Content 1 texts
    [a class="toggle content 3"] toggle 3

    How do I use array? Not really good in JS, so if you can help, that'll be great

    • Sojourner

      I meant:

      [div class="content1"] Content 1 texts
      [a class="toggle content 1"] toggle 1

      [div class="content2"] Content 2 texts
      [a class="toggle content 2"] toggle 2

      [div class="content3"] Content 3 texts
      [a class="toggle content 3"] toggle 3

  9. Nice tutorial. good examples, just trying this instead of using prototype for everything. Am loving jquery at the moment

  10. arthur

    it's called a lambda function because anonymous functions come from a form of mathematical computation called lambda calculus, birth place of most of the functional programming paradigm.

32 Pings

  1. [...] Slicker Show and Hide » Learning jQuery - Tips, Techniques, Tutorials JavaScript example code to show or hide some content using jQuery. (tags: javascript tutorial example web code) [...]

  2. [...] We've received a number of comments recently from people looking for variations on the showing and hiding theme. For the basics, you can take a look at two earlier entries, Basic Show and Hide and Slicker Show and Hide. [...]

  3. [...] idea began trying to combine the article on jQuery “slicker show and hide” and Apple style slider gallery. From there, other effects are added to the mix and dug into the [...]

  4. [...] with a nice little snippet for toggling a div, I have a good collection of plug-ins and bits assembled to create the look and feel I want within [...]

  5. [...] Slicker Show and Hide – Learning jQuery [...]

  6. [...] Slicker Show and Hide » Learning jQuery – Tips, Techniques, Tutorials – Last time I showed you how to make something appear and disappear on a web page. This time I'll show you how to do it with style. [...]

  7. [...] used a slick jQuery solution to toggle the visibility of the slider. The slider is shown by default for the home page and [...]

  8. [...] Accordian – nifty accordian/show-and-hide for cascading content [...]

  9. [...] thanks to Karl Swedberg , his idea is brilliant . Written by ignition in: www [...]

Leave a Comment

IMPORTANT:

  • If you wish to post code examples, please wrap them in <code> tags.
  • Multi-line code should be wrapped in <pre><code> </code></pre>
  • Use &lt; instead of < and &gt; instead of > in the examples themselves. Otherwise, you could lose part of the comment when it's submitted.