How to Create a FAQ Page Using JavaScript

Written by:  • Edited by: Simon Hill
Published Mar 11, 2010
• Related Guides: Javascript

Using JavaScript to enhance pages of your website is a quick and easy way to improve your user's experience. In this article, we will look at one way to use JavaScript to create a FAQ page that is easy to navigate and understand. This article assumes a basic understanding of JavaScript and DOM.

Setting the Groundwork

Before we begin tackling a project in JavaScript, we need to ask ourselves what the driving reason is behind our modification. Depending on our reasons, there are many different approaches to tackling an FAQ page. An FAQ page, by definition, is a listing of frequently asked questions to which we wish to offer answers in a concise and readable manner. With this understanding, we can approach adding some flair to an otherwise boring list by using a little bit of JavaScript.

There are quite literally dozens of different approaches one could take when it comes to interaction with an FAQ page, but we want to hone in and take a close look at a particular method by which we can assist our users in finding the answer to their questions in a timely fashion. For the sake of this article, we will tackle the problem by showing all of our questions in a list and showing only the answer to the selected question when it is clicked. This gives the user complete control over only the information they would like to have access to and avoids any sort of overload. You can see an example of the technique we will be studying in use here.

Have a Pattern in Mind

Good code must be well structured, and this case is no exception. Coming up with a plan and method of attack is essential to developing a page that will be logical and easy to maintain with future modifications. Forward thinking can help a developer from writing themselves into a corner and having to completely rework their pages when things need to be adjusted. One of the best ways to pattern code is simply by extracting as much of the JavaScript from the markup as possible. This allows the pages to be rendered and styled even when JavaScript is disabled - as in the case of screen readers or old browsers.

By allowing for your pages to be displayed properly when JavaScript is not present, you are allowing for accessibility and expansion as your site grows. For this article, we will look at a very well defined structure for our markup followed by precise targeting of our selected elements through JavaScript to show and hide them as needed. To target individual elements with JavaScript, there must be reference points accessible, so we must be sure to appropriately assign attributes to the elements in a manageable way. Keep in mind, too, that we want to be able to select individual elements without scanning the entire document, if possible - thus helping our page load time.

You can see the structure of the markup we will be using in this article here:

FAQ Markup Sample

As it stands, this markup will display well formed FAQs, especially with a bit of CSS applied to make it pop visually. This is precisely what we want to achieve. Now, let's look at how to add our interaction with as minimal a footprint as possible while still allowing for scalability.

Showing page 1 of 3

Comments

Showing all 4 comments
 
Richard Hornsby Mar 15, 2011 10:30 AM
Hang Up Found
Hi Garth, sorry to be a pain, but have a new question.

I implemented the code, but found if there is a hyperlink (say pointing to another page for more information) the question is open upon page load and will not close if the question is clicked on.
Richard Hornsby Mar 12, 2011 1:59 PM
Perfect
Thanks so much, that is exactly what I was looking for.
Garth Henson Mar 12, 2011 12:45 PM
Re: How to Close
Richard, if you wrap the contents of the click handler with a check to see the current state of the clicked question, you can easily determine what to do with it. I've updated the initial example code found here: http://sandbox.guahanweb.com/faq/ with the check for you. Feel free to examine that code change and let me know if there are any additional questions!

-GH
Richard Hornsby Mar 12, 2011 5:07 AM
How to Close
Is there a way to set up the script so that the question-answer couplet will close by clicking on the question a second time? As currently set up, the answer remains open unless a new question is clicked on.

Regardless, the article was very helpful, thanks.
 
blog comments powered by Disqus
Email to a friend