Create Toggle Display (Accordion) with JavaScript
How to build a clickable FAQ-style UI
👀 Live Demo
👇Click on a question title below to toggle its content👇
This is an accordion UI controlled by JavaScript. When you click on the title, the associated content shows or hides accordingly.
This is useful for FAQs, collapsible menus, and showing additional details—great for organizing large amounts of information.
📋 Partial Code (HTML & JavaScript)
This is the minimum HTML and JavaScript needed to implement this functionality.
*Note: CSS classes like `.toggle-box` are assumed to be included in your integrated `style.css` file.
HTML Structure
<!-- First set -->
<div class="toggle-box">
<div class="toggle-title">Question Title 1</div>
<div class="toggle-content">
<p>Answer to question 1 goes here.</p>
</div>
</div>
<!-- Second set -->
<div class="toggle-box">
<div class="toggle-title">Question Title 2</div>
<div class="toggle-content">
<p>Answer to question 2 goes here.</p>
</div>
</div>
JavaScript
document.querySelectorAll('.toggle-title').forEach(title => {
title.addEventListener('click', () => {
title.classList.toggle('active');
const content = title.nextElementSibling;
content.classList.toggle('open');
});
});
💻 Fully Functional Code (HTML File)
Copy the code below and save it as a `.html` file to see it work right away.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toggle Display Demo</title>
<style>
body { font-family: sans-serif; max-width: 600px; margin: 2rem auto; }
.toggle-box { border: 1px solid #ccc; margin-bottom: 1rem; padding: 1rem; border-radius: 8px; }
.toggle-title { cursor: pointer; font-weight: bold; position: relative; }
.toggle-title::after { content: '+'; position: absolute; right: 0; }
.toggle-title.active::after { content: '−'; }
.toggle-content { display: none; margin-top: 1rem; border-top: 1px solid #eee; padding-top: 1rem; }
.toggle-content.open { display: block; }
</style>
</head>
<body>
<h1>FAQ</h1>
<div class="toggle-box">
<div class="toggle-title">Question 1</div>
<div class="toggle-content"><p>Answer 1</p></div>
</div>
<div class="toggle-box">
<div class="toggle-title">Question 2</div>
<div class="toggle-content"><p>Answer 2</p></div>
</div>
<script>
document.querySelectorAll('.toggle-title').forEach(title => {
title.addEventListener('click', () => {
title.classList.toggle('active');
const content = title.nextElementSibling;
content.classList.toggle('open');
});
});
</script>
</body>
</html>
📘 Explanation & Customization Tips
This toggle display template provides a basic structure for creating FAQs or accordion menus. However, in real-world use, multiple sections opening at once can affect readability. If you want only one section to be open at a time, it's recommended to modify the JavaScript to automatically close others when one is opened.
Also, adding animations can make opening/closing transitions smoother and more visually appealing. You can achieve fade or slide effects using CSS transition and max-height.