SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Code a Dynamic Questions & Answers FAQ Page with jQuery

November 23, 2012 by Jake Rocheleau

You can perform many various effects using the jQuery JavaScript library. It’s an open source project which has been gaining followers for a couple years now. Aside from the many jQuery plugins you can build a lot of custom web functionality right from scratch.

I want to use this tutorial to showcase how we can build a custom FAQ webpage layout. I’ll be using a small bit of JavaScript to show and hide the answers. We could include any type of data like static text, images, or videos. Additionally you could port this code into your own layout and custom CSS codes. So without further ado let’s get started!

screenshot preview jQuery CSS3 questions and answers

Live Demo – Download Source Code

Document Header Scripts

To build the demo we need to create a new file named index.html which will serve as the FAQ page. I’ll be using a typical HTML5 doctype with some external header includes. Among some of these is a custom stylesheet named styles.css and a remote font style from Google Web Fonts.

<!doctype html>
<html lang="en-US">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Dynamic Question/Answer FAQ Webpage Layout</title>
  <meta name="author" content="Jake Rocheleau">
  <link rel="shortcut icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="stylesheet" type="text/css" href="styles.css">
  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Paprika">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>

I have also included a reference to the Google-hosted jQuery library. There are some alternate CDNs with jQuery and Microsoft, but any of them will work fine. If you wanted to jazz up the fading animations include a copy of the jQuery UI Library too. You can setup custom easing functions which the animations will mimic.

Inside the body area we also have a very basic code setup. I am using a whole outer wrapper div which contains all our main body content. Then I’ve got questions divided up into divs with the class .q and all internal elements labeled as well.

Body Classes

If you notice on the demo page I’ve split up the questions into three different sections. The script runs on unique IDs so it doesn’t matter where the questions are contained. Therefore you can have a lot of flexibility switching between headers and sub-headers to contain further questions.

<h2>User Accounts</h2>
<div class="q">
  <h3 class="qhead"><a href="#q05">Can I have more than one user account?</a></h3>
  <div class="answer" id="q05"><p>Nope, sorry.</p></div>
</div>

<div class="q">
  <h3 class="qhead"><a href="#q06">How Do I change my user profile photo?</a></h3>
  <div class="answer" id="q06"><p>Access your user Settings panel from the profile screen. Then click on your default profile photo to upload a new image.</p></div>
</div>

Each of the question links are contained inside an h3 tag to promote exclusivity. The anchor link value is a hash related to the answer div’s ID value. This is probably the quickest way to retrieve a value from jQuery, but you may customize this to anything you need.

Inside the .answer div you can place any type of content related to the answer. The whole div is given a CSS display: none; rule to hide everything when the page loads. This shouldn’t interfere with smaller media objects, such as embedded videos or image slideshows.

Simple CSS Styles

Aside from the typical CSS resets I have included some other base properties. This mostly has to do with typography and the page layout styles. But you will notice that the code is very elegant and easy to read. Start off by creating a new file styles.css and include some of the base codes.

/* typography */
h1 { font-family: 'Paprika', Tahoma, Arial, sans-serif; font-size: 2.65em; line-height: 1.55em; margin-bottom: 10px; font-weight: normal; color: #6c635e; }
h2 { font-size: 2.1em; line-height: 1.0em; font-weight: normal; font-style: italic; letter-spacing: 0.05em; color: #778657; margin-bottom: 2px; }
h3 { font-size: 1.45em; }

p { display: block; font-size: 1.4em; line-height: 1.4em; color: #656565; margin-bottom: 15px; }

a { color: #1565ba; text-decoration: none; }
a:hover { text-decoration: underline; } 

First I am setting up our major content for paragraphs, links, and headers. These are the only elements I am positioning inside the FAQ page. But it would be good practice to keep similar typographic styles in the same location within your own stylesheet.

/* wrapper div */
#w { 
display: block; 
margin: 0 auto; 
padding: 18px 25px;
min-width: 400px; 
max-width: 900px; 
background: #fff; 
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px; 
-webkit-box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
-moz-box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
box-shadow: 1px 1px 2px rgba(0,0,0,0.4);
}

/* question containers */
.q { display: block; max-width: 550px; margin-bottom: 16px; }

.qhead { margin-bottom: 3px; }
.qhead a:hover { text-decoration: none; color: #428ddc; border-bottom: 4px solid #c4d3ee; }

.answer { display: none; }

And then finally we have the last section of codes for our FAQ display. I am using some nice CSS3 effects on the container window. You can notice they really stand out on the page and against the background. Also I have the question containers limited at 550px width to save space. In any other ideal website layout you would probably have a sidebar or two which would limit the body width.

Creating the Fade Effect

This last part is accomplished with just a few lines of jQuery and shouldn’t be difficult to follow. Instead of creating a new document I have simply embedded this code into a script tag right before closing the page body.

$(document).ready(function(){
  $(".qhead a").on("click", function(e){
    e.preventDefault();
    var href = $(this).attr("href");
    
    $(href).fadeToggle(450);
  });
});

After the document finishes loading then I am placing a click event listener on the .qhead links. This means whenever the user clicks on any of our header links we call the internal function code. It starts by targeting the click event variable e and using the preventDefault() method. This is how we stop the page from loading each HREF value into the URL bar.

The other code is targeting the href attribute value on the clicked link and using this as a new jQuery selector. This means we can quickly target the proper answer div and use .fadeToggle() to hide or show content as needed. Obviously a much easier solution than trying to determine if the paragraph is already displaying to hide or show accordingly.

screenshot preview jQuery CSS3 questions and answers

Live Demo – Download Source Code

Final Thoughts

This tutorial is a bit self-explanatory if you are familiar with JavaScript development. But even some intermediate web developers could find this handy for duplicating the code on future projects. Building an FAQ page is a great way to offer solutions to your visitors and explain the purpose of your website.

I hope this tutorial can offer a great resource to some web designers & developers. You can download a copy of my project source code from the links above. Feel free to play around and see if you can build any additional features.

Filed Under: Tutorial Tagged With: coding, css3, howto, html5, javascript, jquery, web development

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022