How to create a FAQ page using JavaScript

A Frequently Asked Questions (FAQ) page is essential for websites, helping users find answers to common questions quickly. This tutorial shows how to create an interactive FAQ page using JavaScript with an accordion-style interface.

Overview

FAQ pages reduce support requests by providing instant answers to common questions. The most effective approach is using an accordion component where questions can be clicked to reveal or hide answers.

Approach

We'll create an accordion-style FAQ using HTML structure, CSS styling, and JavaScript functionality. Each FAQ item will toggle between expanded and collapsed states when clicked.

Implementation Steps

Step 1: Create the HTML structure with a container for FAQ items.

Step 2: Define FAQ data as an array of question-answer objects.

Step 3: Generate HTML dynamically using JavaScript to render each FAQ item.

Step 4: Add click event listeners to enable accordion functionality.

Complete Example

<!DOCTYPE html>
<html>
<head>
    <title>FAQ Page - TutorialsPoint</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .faq-container {
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .faq-title {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 2.5rem;
        }
        
        .faq-item {
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            overflow: hidden;
        }
        
        .faq-question {
            background: #40a944;
            color: white;
            padding: 20px;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-weight: bold;
            transition: background-color 0.3s ease;
        }
        
        .faq-question:hover {
            background: #369c3a;
        }
        
        .faq-answer {
            padding: 20px;
            background: #f9f9f9;
            display: none;
            border-top: 1px solid #eee;
        }
        
        .toggle-icon {
            font-size: 1.5rem;
            transition: transform 0.3s ease;
        }
        
        .faq-item.active .toggle-icon {
            transform: rotate(45deg);
        }
    </style>
</head>
<body>
    <div class="faq-container">
        <h1 class="faq-title">Frequently Asked Questions</h1>
        <div id="faq-content"></div>
    </div>

    <script>
        // FAQ data array
        const faqData = [
            {
                question: "How to join training programs on TutorialsPoint?",
                answer: "Visit our website at <a href='https://www.tutorialspoint.com'>tutorialspoint.com</a> and browse our course catalog. Click on any course to enroll and start learning immediately."
            },
            {
                question: "Which courses are available on TutorialsPoint?",
                answer: "We offer over 1000+ courses covering programming languages, web development, data science, mobile development, and more. Complete training programs are available with certificates."
            },
            {
                question: "What are the benefits of annual membership?",
                answer: "Annual membership gives you access to 5500+ premium video tutorials, downloadable content, ad-free experience, and priority support. Join now for unlimited learning."
            },
            {
                question: "Who creates the content for TutorialsPoint?",
                answer: "Our content is created by highly skilled professionals and subject matter experts. We have a dedicated team in Hyderabad, India, including technical writers, developers, and designers."
            },
            {
                question: "Do I get certificates after completing courses?",
                answer: "Yes! Upon successful completion of any course, you receive a certificate that you can share on LinkedIn or include in your resume to showcase your skills."
            }
        ];

        // Generate FAQ HTML
        function generateFAQHTML() {
            let htmlContent = '';
            
            faqData.forEach((item, index) => {
                htmlContent += `
                    <div class="faq-item">
                        <div class="faq-question">
                            <span>${item.question}</span>
                            <span class="toggle-icon">+</span>
                        </div>
                        <div class="faq-answer">
                            ${item.answer}
                        </div>
                    </div>
                `;
            });
            
            return htmlContent;
        }

        // Add click functionality
        function addAccordionFunctionality() {
            const faqItems = document.querySelectorAll('.faq-item');
            
            faqItems.forEach(item => {
                const question = item.querySelector('.faq-question');
                const answer = item.querySelector('.faq-answer');
                
                question.addEventListener('click', () => {
                    // Close all other items
                    faqItems.forEach(otherItem => {
                        if (otherItem !== item) {
                            otherItem.classList.remove('active');
                            otherItem.querySelector('.faq-answer').style.display = 'none';
                        }
                    });
                    
                    // Toggle current item
                    const isActive = item.classList.contains('active');
                    
                    if (isActive) {
                        item.classList.remove('active');
                        answer.style.display = 'none';
                    } else {
                        item.classList.add('active');
                        answer.style.display = 'block';
                    }
                });
            });
        }

        // Initialize FAQ page
        function initializeFAQ() {
            const faqContainer = document.getElementById('faq-content');
            faqContainer.innerHTML = generateFAQHTML();
            addAccordionFunctionality();
        }

        // Load FAQ when page loads
        document.addEventListener('DOMContentLoaded', initializeFAQ);
    </script>
</body>
</html>

How It Works

The solution uses three main components:

  1. Data Structure: FAQ items stored as objects with question and answer properties
  2. Dynamic Generation: JavaScript creates HTML for each FAQ item using template literals
  3. Event Handling: Click listeners toggle answer visibility and manage accordion state

Key Features

  • Responsive Design: Works on desktop and mobile devices
  • Smooth Animations: CSS transitions for better user experience
  • One-at-a-time: Only one FAQ answer shows at a time
  • Hover Effects: Visual feedback when hovering over questions

Customization Options

You can easily customize the FAQ page by:

  • Modifying the faqData array to add/remove questions
  • Changing CSS colors and styling to match your brand
  • Adding icons or images to enhance visual appeal
  • Integrating with a database or API for dynamic content

Conclusion

This JavaScript-powered FAQ page provides an interactive way to display common questions and answers. The accordion interface keeps the page organized while allowing users to quickly find the information they need. You can extend this foundation by connecting to APIs or databases for dynamic content management.

Updated on: 2026-03-15T23:19:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements