Reader small image

You're reading from  jQuery for Designers Beginner's Guide Second Edition

Product typeBook
Published inJul 2014
Reading LevelBeginner
Publisher
ISBN-139781783284535
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Natalie Maclees
Natalie Maclees
author image
Natalie Maclees

contacted on 10 may '16 _______________ Natalie MacLees is the founder of Purple Pen Productions (purplepen.com), an interactive agency based in Los Angeles, California. She has been designing websites since 1997 and is a passionate advocate of both accessibility and usability. She loves teaching and sharing her knowledge, both in seminars and workshops and also with her clients. She discovered WordPress a few years ago as a flexible, extendable, and quick way to build robust websites that clients could manage on their own. She is the organizer of the Southern California WordPress Meetup Group. She is also a Google Analytics Qualified Individual.
Read more about Natalie Maclees

Right arrow

Chapter 3. Making a Better FAQ Page

The Frequently Asked Questions (FAQ) page has been a mainstay of all types of websites since the dawn of the Web. It's used as a marketing page, as an attempt to reduce the number of calls or e-mails to a customer service department and as a helpful tool for site visitors to learn more about the company or organization they're dealing with or the products or services they're interested in purchasing.

Though we'll be building an FAQ page, for this example, the expand and collapse techniques will be useful in many different situations—a list of events with event details, a listing of staff or members with bios, a list of products with details—any situation where a listing of items should be quick and easy for site visitors to scan, but where more information should be readily and easily available upon demand when they find the thing they're looking for.

In this chapter, you'll learn:

  • How to traverse an HTML document with jQuery

  • How to show and hide elements...

Marking up the FAQ page


We'll get started by taking some extra care and attention with the way we mark up our FAQ list. As with most things that deal with web development, there's no right way of doing anything, so don't assume this approach is the only correct one. Any markup that makes sense semantically and makes it easy to enhance your list with CSS and JavaScript is perfectly acceptable.

Time for action – setting up the HTML file


Perform the following steps to get the HTML file set up for our FAQ page:

  1. We'll get started with our sample HTML file and associated files and folders, like we set up in Chapter 1, Designer, Meet jQuery. In this case, our HTML page will contain a definition list with the questions inside the <dt> tags and the answers wrapped in the <dd> tags. By default, most browsers will indent the <dd> tags, which means the questions hang into the left margin, making them easy to scan. Inside the <body> tag of your HTML document, add a heading and a definition list as shown in the following code:

    <h1>Frequently Asked Questions</h1>
    <dl>
      <dt>What is jQuery?</dt>
      <dd>
        <p>jQuery is an awesome JavaScript library</p>
      </dd>
    
      <dt>Why should I use jQuery?</dt>  
      <dd>
        <p>Because it's awesome and it makes writing JavaScript faster and easier</p>
    ...

Time for action – moving around an HTML document


Perform the following steps to move from one element to another in JavaScript:

  1. We're going to keep working with the files we set up in the previous section. Open up the scripts.js file that's inside your scripts folder. Add a document ready statement, then write a new empty function called dynamicFAQ, as follows:

    $(document).ready(function(){
    });
    
    function dynamicFAQ() {
      // Our function will go here  
    }
  2. Let's think through how we'd like this page to behave. We'd like to have all the answers to our questions hidden when the page is loaded. Then, when a user finds the question they're looking for, we'd like to show the associated answer when they click on the question.

    This means the first thing we'll need to do is hide all the answers when the page loads. We can do this just like we did with the tab exercise in the Chapter 2, Enhancing Links. Get started by adding a class jsOff to the <body> tag, as follows:

    <body class="jsOff">

    Now...

Sprucing up our FAQ page


That was so easy, in fact, that we have plenty of time left over to enhance our FAQ page to make it even better. This is where the power of jQuery becomes apparent—you can not only create a show/hide FAQ page, but you can make it a fancy one and still meet your deadline. How's that for impressing a client or your boss?

Time for action – making it fancy


Perform the following steps to add some fancy new features to the FAQ page:

  1. Let's start with a little CSS code to change the cursor to a pointer and add a little hover effect to our questions to make it obvious to site visitors that the questions are clickable. Open up the styles.css file that's inside the styles folder and add the following bit of CSS code:

    .jsOn dt {
      cursor: pointer;
    }
    
    .jsOn dt:hover {
      color: #ac92ec;
    }

    We're only applying these styles for those site visitors that have JavaScript enabled. These styles definitely help to communicate to the site visitor that the questions are clickable. You might also choose to change something other than the font color for the hover effect. Feel free to style your FAQ list however you'd like. Have a look at the following screenshot:

  2. Now that we've made it clear that our <dt> elements can be interacted with, let's take a look at how to show the answers in a nicer way. When we click on a question to...

We're almost there!


jQuery made animating that show and hide so easy that we've still got time left over to enhance our FAQ page even more. It would be nice to add some sort of indicator to our questions to show that they're collapsed and can be expanded, and to add some sort of special style to our questions once they're opened to show that they can be collapsed again.

Time for action – adding some final touches


Perform the following steps to add some finishing touches to our FAQ list:

  1. Let's start with some simple CSS code to add a small arrow icon to the left side of our questions. Head back into style.css and modify the styles a bit to add an arrow as follows:

    .jsOn dt:before {
      border: 0.5em solid;
      border-color: transparent transparent transparent #f2eeef;
      content: '';
      display: inline-block;
      height: 0;
      margin-right: 0.5em;
      vertical-align: middle;
      width: 0;
    }
    
    .jsOn dt:hover:before {
      border-left-color: #ac92ec;
    }

    You might be wondering about this sort of odd bit of CSS. This is a technique to create triangles in pure CSS without having to use any images. If you're not familiar with this technique, I recommend checking out appendTo's blog post that explains pure CSS triangles at http://appendto.com/2013/03/pure-css-triangles-explained/.

    We've also included a hover style so that the triangle will match the text color when the site visitor...

Summary


In this chapter, you learned how to set up a basic FAQ page that hides the answers to the questions until the site visitor needs to see them. Because jQuery made this so simple, we had plenty of time left over to enhance our FAQ page even more, adding animations to our show and hide phenomenon for the answers, and taking advantage of CSS to style our questions with special open and closed classes to communicate to our site visitors how our page works. And we did all of that with just a few lines of code!

Next, we'll learn how to build an interactive drop-down navigation menu.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
jQuery for Designers Beginner's Guide Second Edition
Published in: Jul 2014Publisher: ISBN-13: 9781783284535
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Natalie Maclees

contacted on 10 may '16 _______________ Natalie MacLees is the founder of Purple Pen Productions (purplepen.com), an interactive agency based in Los Angeles, California. She has been designing websites since 1997 and is a passionate advocate of both accessibility and usability. She loves teaching and sharing her knowledge, both in seminars and workshops and also with her clients. She discovered WordPress a few years ago as a flexible, extendable, and quick way to build robust websites that clients could manage on their own. She is the organizer of the Southern California WordPress Meetup Group. She is also a Google Analytics Qualified Individual.
Read more about Natalie Maclees