Shaun Mccran

My digital playground

04
M
A
Y
2010

JQuery multiple show and hide div’s based on click event

This article shows how to use JQuery to show and hide many div's at the same time, revealing the content that the user requests. It also degrades gracefully if you turn off Javascript.

Have you ever tried to display a Questions and Answers type page, where the Question is a clickable link that shows the corresponding Answer (FAQ's)? It is quite tricky to make this interesting, and Accessible to impaired users.

A full demo of multiple show and hide divs is here: link

The reason I started trying to develop this functionality is that I couldn't use a select field jump event, as that is useless for screen readers, and displaying an old school header list with bookmark href links is just shockingly bad. This is Accessible, and more interesting to use.

My content is dynamically generated from a query, but here is an example of what it looks like:

view plain print about
1<cfoutput>
2<h2>
3<a href="javascript:void(0);" id="showAnswer" class="showAnswer" ident="1">
4How far away is the Moon?</a>
5</h2>
6
7<div class="answers" id="answers1">
8I think its about 23km away.
9</div>
10
11<h2>
12<a href="javascript:void(0);" id="showAnswer" class="showAnswer" ident="2">
13Where is the coldest place on Earth?</a>
14</h2>
15
16<div class="answers" id="answers2">
17The coldest place, known as 'Ridge A' is located deep within the Antarctic Plateau. It has an average winter temperature of minus 94 degrees Fahrenheit (minus 70 degrees Celsius).
18</div>
19
20<h2>
21<a href="javascript:void(0);" id="showAnswer" class="showAnswer" ident="3">
22Golden Delicious, Granny Smith and Gala are types of which fruit?</a>
23</h2>
24
25<div class="answers" id="answers3">
26They are all types of Apple.
27</div>
28</cfoutput>

Each of the links has a unique ident attribute value (1,2,3) this is used in the following JQuery script to determine which child div to toggle.

Start off by calling the JQuery library from Google. Next we have a function that loads when the rest of the DOM is ready.

Next we hide all div's that have and id that contains the string 'answers'. In this way when the page loads all the divs are hidden.

Next we have our click event, which is based on a selector for the 'showAnswer' class. This will hide all the divs in the same way as above, to make sure we don't have tow div's open at the same time. Next it gets the ident value of the clicked link, and builds a selector to toggle.

view plain print about
1<s/cript type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js"></script>
2
3<s/cript language="javascript">
4$(document).ready(function() {
5
6    // hide all the div elements with answers in the class name
7    $("div[id^='answers']").hide();
8
9    // toggles the divs on click of the links
10    $('.showAnswer').click(function() {
11
12    // hide all the divs
13    $("div[id^='answers']").hide(200);
14
15    var ident = $(this).attr('ident');
16
17    // show the clicked div
18    $('#answers' + ident).toggle(200);
19
20        return false;
21    });
22});
23</script>

I briefly looked at using the child() or next() JQuery methods so that I didn't need a unique identifier to do this, but didn't get either of those working in anywhere near as simple a way as this. (was 15-20 lines of code)

If anyone knows a more modular, detached way of doing this it would be good to see.

A full demo of multiple show and hide divs is here: link

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Tony Nelson's Gravatar Here's another quick way, using definition lists:

<InvalidTag type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/...;
<InvalidTag type="text/javascript">
   $(document).ready(function() {
      $('dd').hide();
      $('dt').click(function() {
         $('dd').hide(200);
         $(this).next().show(200);
      });
   });
</script>

<dl>
   <dt>How far away is the Moon?</dt>
   <dd>I think its about 23km away.</dd>
   <dt>Where is the coldest place on Earth?</dt>
   <dd>The coldest place, known as 'Ridge A' is located deep within the Antarctic Plateau. It has an average winter temperature of minus 94 degrees Fahrenheit (minus 70 degrees Celsius).</dd>
   <dt>Golden Delicious, Granny Smith and Gala are types of which fruit?</dt>
   <dd>They are all types of Apple.</dd>
</dl>
# Posted By Tony Nelson | 04/05/2010 16:47
Shaun McCran's Gravatar Hi Tony,
I like that! Thats much neater than mine! I was looking for a way to de-couple the JQuery from any sort of index value passed into the query loop. That seemed a bit old school JavaScript.

Its really impressive that this functionlity is only four lines long!

Thanks
Shaun
# Posted By Shaun McCran | 04/05/2010 17:02
jenna's Gravatar Developers seem to own the skill of making this complex process very easy and my site does have an FAQ build as described in this post. After reading the post and getting more information from http://delegate-your-assignment.com/ I understand now the difficulty of having such program installed. JQuery is just awesome and the fact that they are making it look simple is all the more admirable.
# Posted By jenna | 09/11/2015 21:49
Back to top