How to create an FAQ section to any website using JavaScript ? Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 "accordion" and store them in the variable answers. This assumes that these elements represent the accordion items.Event Listener Iteration:Iterate over each element in the answers NodeList using forEach.Event Listener Registration:Attach a click event listener to each accordion element.Toggle Class:Inside the event listener, check if the current element (event) has the class "active" using classList.contains.If it does, remove the "active" class; otherwise, add the "active" class.Accordion Toggle:The logic toggles the "active" class, allowing the accordion items to expand or collapse based on their current state.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> <script src="main.js"></script> </head> <body> <h2 style="color:green; text-align:center"> GeeksforGeeks </h2> <div class="layout"> <div class="accordion"> <div class="accordion__question"> <p>Where is Taj Mahal located?</p> </div> <div class="accordion__answer"> <p>Taj Mahal is located in Agra, Uttar Pradesh.</p> </div> </div> <div class="accordion"> <div class="accordion__question"> <p>How many planets are there in solar system?</p> </div> <div class="accordion__answer"> <p> There are eight planets in solar system. Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune. </p> </div> </div> </div> </body> </html> CSS body { background-color: rgb(153, 218, 196); } .layout { width: 600px; margin: auto; } .accordion { padding: 10px; margin-top: 10px; margin-bottom: 10px; background: rgb(105, 206, 105); border-radius: 10px; } .accordion__question p { margin: 5px; padding: 0; font-family: Verdana; font-size: 20px; } .accordion__answer p { margin: 5px; padding: 10px; font-size: large; font-family: Verdana, Geneva, Tahoma, sans-serif; color: rgb(255, 255, 255); background: rgb(82, 170, 122); border-radius: 10px; } .accordion:hover { cursor: pointer; } .accordion__answer { display: none; } .accordion.active .accordion__answer { display: block; } JavaScript let answers = document.querySelectorAll(".accordion"); answers.forEach((event) => { event.addEventListener("click", () => { if (event.classList.contains("active")) { event.classList.remove("active"); } else { event.classList.add("active"); } }); }); OutputFAQ feature Comment More infoAdvertise with us Next Article How to create an FAQ section to any website using JavaScript ? A anuragsingh1022 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Create a Portfolio Website using HTML CSS and JavaScript ? A portfolio website is a website that represents you online on the web pages. It contains different sections like Introduction to Yourself, About You, Your Services, Your Contact Details, and Your Portfolio. We are going to build all of the sections with an interactive and attractive web design that 15+ min read How to create a FAQ page using JavaScript ? 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 pa 6 min read Create a website using HTML CSS and JavaScript that stores data in Firebase Following are some simple steps in order to connect our static Web Page with Firebase. Step-by-Step ImplementationStep 1: Firstly, We are going to create a project on Firebase to connect our static web page. Visit the Firebase page to configure your project. Visit the website and click the On Add P 3 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 Create a Wikipedia Search using HTML CSS and JavaScript In this article, we're going to create an application, for searching Wikipedia. Using HTML, CSS, and JavaScript, users will be able to search for articles on Wikipedia and view the results in a user interface. When the user inputs the search text into the textbox, the search result for the same will 3 min read Like