How to create a FAQ page using JavaScript ?
Last Updated :
28 Apr, 2022
The frequently Asked Questions (FAQ) section is one of the most important sections of any website, especially if you are providing services. If you want to learn how to make it by yourself then welcome! today we'll learn how to create a FAQ page using JavaScript.
Functionalities required in a FAQ page:
- Initially, only questions are displayed where each question is separated by a line.
- On clicking plus icon, the answer section is expanded and an answer to a particular question is displayed.
- When a plus icon is clicked and an answer section is displayed, a cross icon replaces the plus icon.
- When a cross icon is clicked answer section collapse and the cross icon is again replaced by a plus icon.
HTML and CSS will take care of the UI and javascript will help us in achieving the above functionalities, So let's get started.
HTML: You can use different HTML tags to create your page skeleton, here you have used the unordered list to create a page skeleton.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
integrity=
"sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
crossorigin="anonymous" referrerpolicy="no-referrer"/>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<div class="container">
<h1>FAQs</h1>
<ul>
<li class="faq">
<div class="question">
Which is the best portal to study
Computer Science?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<div class="answer non-active">
GeeksforGeeks is the best Computer
Science portal for geeks. It contains
well written, well thought and well
explained computer science and
programming articles.
</div>
</li>
<li class="faq">
<div class="question">
What is a FAQ section?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<p class="answer non-active">the Frequently
Asked Questions (FAQ) section is
a part of your website where you
address common concerns, questions,
and objections that customers have.
</p>
</li>
<li class="faq">
<div class="question">
What should be included in a FAQ section?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<p class="answer non-active">
Fully answer the question, don't
just link to a different page.
</p>
</li>
</ul>
</div>
<script src="script.js"></script>
</body>
</html>
Â
Note: Here we have used FontAwesome icons for icons and google fonts for new font styles.
CSS
@import url(
"https://fonts.googleapis.com/css2?family=Lato&family=Roboto+Slab&family=Spartan:wght@400;700&display=swap");
.container {
position: absolute;
top: 20%;
left: 50%;
transform: translate(-50%, -20%);
width: 60%;
}
h1 {
text-align: center;
font-family: "Roboto Slab", serif;
font-size: 40px;
}
li {
list-style: none;
margin-bottom: 10px;
padding: 15px;
border-bottom: 2px solid #6d6b6b;
}
.question {
font-family: "Spartan", sans-serif;
font-size: 20px;
font-weight: 600;
margin-bottom: 10px;
position: relative;
color: #5c5a5a;
}
.icon-main {
position: absolute;
right: 0px;
cursor: pointer;
}
.non-active {
display: none;
}
.answer{
font-size: 17px;
font-family: "Spartan", sans-serif;
}
Here a "non-active" class has assigned a CSS property display: none which is responsible for expanding and collapsing of answer section.
JavaScript:
JavaScript
const faqs = document.querySelectorAll(".faq");
for (const item of faqs) {
const curr_faq = item.childNodes;
const question = curr_faq[1];
const answer = curr_faq[3];
const icon = question.querySelector(".icon-main");
icon.addEventListener("click", function () {
answer.classList.toggle("non-active");
const i = icon.querySelector("i");
i.classList.toggle("fa-xmark");
i.classList.toggle("fa-plus");
console.log(i);
});
}
Approach:
- Variable faqs contain an array of all <li> elements which depends on the number of questions on a FAQ page. Since we want each question and answer pair to possess the same functionalities therefore we have to loop over this faqs array and add the following properties to each li element.
- Since we are looping over li elements and question and answer divs are the child element of it, so to extract them we can use the childNodes property which returns an array, and below are its contents.
 - Since we are interested only in div.question and div.answer we extract the first and the third element of the curr_faq array.
- You can also extract the child elements from the parent element by using the querySelector method on the parent element which we had also used to get the icon element.
- If the answer element contains a non-active class(which means that currently the answer section is collapsed) then remove it when the plus icon is clicked. Similarly, if the answer element does not contain the non-active class(which means that currently the answer section is expanded) then add it when the cross icon is clicked.
- You can use classList.add() and classList.remove() methods to remove and add a non-active class which provides us expansion and collapse features over an answer element. But I prefer to use classList.toggle() method which does both works together for us and reduces some lines of code.
- Also, add and remove fa-plus and fa-xmark classes whenever required to toggle between plus and cross icons.
Hurray! our FAQ page is ready. Let's combine our HTML, CSS, and Javascript code to see what our page looks like.
Final Code:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
integrity=
"sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
@import url(
"https://fonts.googleapis.com/css2?family=Lato&family=Roboto+Slab&family=Spartan:wght@400;700&display=swap");
.container {
position: absolute;
top: 20%;
left: 50%;
transform: translate(-50%, -20%);
width: 60%;
}
h1 {
text-align: center;
font-family: "Roboto Slab", serif;
font-size: 40px;
}
li {
list-style: none;
margin-bottom: 10px;
padding: 15px;
border-bottom: 2px solid #6d6b6b;
}
.question {
font-family: "Spartan", sans-serif;
font-size: 20px;
font-weight: 600;
margin-bottom: 10px;
position: relative;
color: #5c5a5a;
}
.icon-main {
position: absolute;
right: 0px;
cursor: pointer;
}
.non-active {
display: none;
}
.answer {
font-size: 17px;
font-family: "Spartan", sans-serif;
}
</style>
<title>FAQs</title>
</head>
<body>
<div class="container">
<h1>FAQs</h1>
<ul>
<li class="faq">
<div class="question">
Which is the best portal to study
Computer Science?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<div class="answer non-active">
GeeksforGeeks is the best Computer
Science portal for geeks. It contains
well written, well thought and well
explained computer science and
programming articles.
</div>
</li>
<li class="faq">
<div class="question">
What is a FAQ section?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<p class="answer non-active">
the Frequently Asked Questions (FAQ)
section is a part of your website
where you address common concerns,
questions,and objections that
customers have.
</p>
</li>
<li class="faq">
<div class="question">
What should be included in a FAQ section?
<span class="icon-main">
<i class="fa-solid fa-plus"></i>
</span>
</div>
<p class="answer non-active">
Fully answer the question, don't
just link to a different page.
</p>
</li>
</ul>
</div>
<script>
const faqs = document.querySelectorAll(".faq");
for (const item of faqs) {
const curr_faq = item.childNodes;
console.log(curr_faq);
const question = curr_faq[1];
const answer = curr_faq[3];
const icon = question.querySelector(".icon-main");
icon.addEventListener("click", function () {
answer.classList.toggle("non-active");
const i = icon.querySelector("i");
i.classList.toggle("fa-xmark");
i.classList.toggle("fa-plus");
});
}
</script>
</body>
</html>
Output:
Â
Similar Reads
How to create a Landing page using HTML CSS and JavaScript ? A landing page, also referred to as a lead capture page, static page, or destination page, serves a specific purpose and typically appears as a result of online advertising or search engine optimization efforts. Unlike a homepage, a landing page is stripped of distractions and focuses on capturing v
7 min read
How to create editable div using JavaScript ? In this article, we will be explaining to you how to create an editable div using HTML, CSS, and JavaScript. An editable div is one on which is you will click then it will generate an editable text area to edit or to write any text on your browser itself. After editing, when you click on somewhere e
3 min read
How to create an FAQ section to any website using JavaScript ? We will create a Frequently Asked Questions(FAQ) accordion using JavaScript. The accordion is used to display the content in list format. It can expand or collapse to display the content it contains.ApproachSelection of Elements:Use document.querySelectorAll to select all elements with the class "ac
2 min read
How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit
2 min read
How to print a web page using JavaScript ? Javascript is the world most popular lightweight, cross-platform, interpreted compiled programming language, along with a scripting language for web. It facilitates the dynamic functionality that helps to make the interactive website. As all the Morden browsers use the Javascript & is a client-s
2 min read