FAQ Accordion

Concepts Used: SlideToggle, event handling

Features:

  • Click a question → answer expands/collapses
<!DOCTYPE html>
<html>
<head>
  <title>FAQ Accordion</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <style>
.question { cursor: pointer; font-weight: bold; margin: 10px 0; }
.answer { display: none; margin-left: 20px; }
</style> </head> <body> <h2>FAQ</h2> <div class="question">What is jQuery?</div> <div class="answer">jQuery is a JavaScript library for DOM manipulation.</div> <div class="question">Why use jQuery?</div> <div class="answer">It simplifies JavaScript code and works across browsers.</div> <script>
$(document).ready(function(){
  $(".question").click(function(){
    $(this).next(".answer").slideToggle();
  });
});
</script> </body> </html>

Explanation:

  • .slideToggle() smoothly shows/hides answers.
  • .next() selects the answer related to the clicked question.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *