Create a fun, clean, easy to use FAQ list with jQuery!

jQuery FAQ List

This tutorial demonstrates how to create a jQuery FAQ list that is both easy to use and easy to create. View a real life example.

FAQ pages are often a long list of questions followed by answers, or a list of questions that anchor link to an answer further down the page. Neither one creates a very usable way for users to easily find the information they are looking for.

The following script fixes all of that by creating a clean page that puts the user in control of what information to display.

The HTML

First we wrap everything in a wrapper div and give it a class name. Within this div is an h5. This h5 is actually our question. It doesn't have to be an h5, but if you use a different element, you will have to edit the jQuery accordingly to target the element you have chosen to use. Next, we have a div with a class of answer. We will be targeting this div in order for the user to be able to show and hide the answer. Within this div is a paragraph tag with some filler lorem ipsum text.

That is all you need to create the HTML for one question, however, to get the full effect, you will want to copy and past this code a few times so that you have a few questions.

<div class="FAQWrap">
  <h5>This is my question?</h5>
  <div class="answer">
    <p>Notare quam littera gothica quam, nunc putamus parum claram anteposuerit litterarum. Et quinta decima eodem modo typi qui nunc. Per seacula quarta decima nobis videntur parum clari: fiant sollemnes in.  Equitur mutationem consuetudium lectorum mirum est formas humanitatis.</p>
    </div>
  </div>

The CSS

The styles are essentially unimportant. You can styles the layout any way you want, but I will give some insight into some of the styles I often choose when creating an FAQ page.

The first thing to note is that I give my h5 a cursor: pointer style. This creates the hand pointer when the user hovers on the question. That gives the user a clear indication that something will happen if they click on this question.

You will also see that I have different background images for the h5 depending on whether it has a class of inactive or active. We will change this class with jQuery when the user clicks the question and the class, along with the background image arrow, will toggle, again creating visual interactivity between the user and the page.

#content {width: 960px; margin: 0 auto;}
.FAQWrap h5 {color: #555; margin: 10px 0 0 0; padding: 0 0 0 20px; font-size: 14px; font-weight: normal; cursor: pointer;}
.FAQWrap h5.inactive {background: url(../images/bg-faq-inactive.png) no-repeat 0 3px;}
.FAQWrap h5.active {background: url(../images/bg-faq-active.png) no-repeat 0 3px;}
.FAQWrap .answer {background: #EEE; margin: 10px 10px 20px 30px; padding: 10px;}

The jQuery

And finally, the most important part, the jQuery. Remember of course to first link to the jQuery framework.

The first thing we do is hide all answer divs when the page loads. That creates the nice clean layout we are looking for and puts the control in the hands of the user as which information they want displayed on the page.

Next, we target all h5's within the FAQWrap div and assign a class of inactive. This, along with our CSS on the h5, will create a background image of an arrow pointing to the right, denoting that the answer is hidden, but can be displayed if you click the question.

The next thing we want to do is run a function when we click on an h5 within FAQWrap. The first thing we do is use this toggleClass method to switch the class of the actual h5 we clicked. toggleClass simply toggles the class name on the targeted element. If it has a class of 'inactive', it will switch the class to 'active', and if it is 'active', it will switch the class to 'inactive'. So, hopefully you are now beginning to see that each time we click on an h5, we are switching the class name between 'inactive' and 'active', and that, in combination with our CSS, is what is changing our background image arrow on the h5.

The last thing we do, which is most important, is slideToggle the answer div. The way we do that is first find the parent of 'this' (the h5 we are clicking on), then we are finding the div with a class of answer within that parent element, and slideToggling it. This means that if the answer is showing, it will slideUp and and be hidden, and if it is hidden, it will slideDown and show.

$(function() {
      $('.answer').hide();
      $('.FAQWrap h5').addClass('inactive');
      $('.FAQWrap h5').click(function() {
        $(this).toggleClass('active');
        $(this).toggleClass('inactive');
        $(this).parent().find('.answer').slideToggle();        
      });
    });

The Demo

And so when you put all of that together, you get a nice, clean, easy to use FAQ page that any user will enjoy using!

This is my question?

Notare quam littera gothica quam, nunc putamus parum claram anteposuerit litterarum. Et quinta decima eodem modo typi qui nunc. Per seacula quarta decima nobis videntur parum clari: fiant sollemnes in. Sequitur mutationem consuetudium lectorum mirum est formas humanitatis.

This is my question?

Notare quam littera gothica quam, nunc putamus parum claram anteposuerit litterarum. Et quinta decima eodem modo typi qui nunc. Per seacula quarta decima nobis videntur parum clari: fiant sollemnes in. Sequitur mutationem consuetudium lectorum mirum est formas humanitatis.

This is my question?

Notare quam littera gothica quam, nunc putamus parum claram anteposuerit litterarum. Et quinta decima eodem modo typi qui nunc. Per seacula quarta decima nobis videntur parum clari: fiant sollemnes in. Sequitur mutationem consuetudium lectorum mirum est formas humanitatis.

Download the source

2 Comments

Posted November 7, 2009 at 12:51 pm | Permalink

This is great!

Is there a way to create a Toggle For Show/Hide All answers?

Thanks.
kevin


Goon
Posted November 8, 2009 at 7:14 pm | Permalink

Yeah for sure, let me know if this makes sense…

Rather than targeting the answer associated with each h5 using ‘this’, you could be less specific and get all .answer divs when you click on a show/hide button. (See if below makes sense). Then you could just have a button that hides them all that would be the same function just with slideUp instead of slideDown. I think that would work, make sense? I can put together an actual working example if you need further instructions.

$(function() {
$(‘.answer’).hide();
$(‘class of show/hide button’).click(function() {
$(.answer).slideDown();
});
});

Post a Comment

Your email is never shared. Required fields are marked *