Can you help me create a CSS Toggle similar to this design?

I am currently working on a website and I need to create a FAQs page with a specific layout.

When a user clicks on a question (Q), the answer (A) should open smoothly using jQuery animation. Only one question should be visible at a time.

If a user clicks on another question, the previous one should close automatically and the newly clicked question will be opened.

<div class="faqBlock">

    <div class="qa">
        <dt>How can I purchase Snoopra?</dt>
        <dd>To buy Snoopra, simply choose between monthly or yearly payment options, click on the buy now button, enter your payment information, and you're good to go!</dd>
    </div><!-- qa -->

</div><!-- faqBlock -->

Answer №1

We have found a solution to the issue you raised. We believe it meets your requirements.

http://jsfiddle.net/ardeezstyle/ESLEB/1/

$(document).delegate('.q','click',function(){
    $('.a').hide();
    $(this).next('.a').show();
});

Answer №2

Introducing the amazing accordion effect and the simplest way to create it:

$('.faq-question').click(function(){
  $(this).next().slideToggle();
});
.faq-question > *{margin:0; padding:8px 16px;} /* all */
.faq-question > h3{cursor:pointer; border-top: 1px solid #eee;} /* questions */
.faq-question > div{display:none;} /* Hide all answers */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faq-question">
  <h3>Question 1</h3>
  <div>answer 1...</div>
  <h3>Question 2</h3>
  <div>answer 2...</div>
  <h3>Question 3</h3>
  <div>answer 3...</div>
</div>

To easily toggle between questions:

$('.faq-question').each(function() {
  
  var $question = $(this).find("> h3"),
      $answer = $(this).find("> div");

  $question.click(function() {
    var $faq = $(this).next(); // Get the question's answer
    $answer.not($faq).slideUp();    // Close all answers (not the clicked one)
    $faq.slideToggle();        // Toggle answer
  });

});
.faq-question > *{margin:0; padding:8px 16px;} /* all */
.faq-question > h3{cursor:pointer; border-top: 1px solid #eee;} /* questions */
.faq-question > div{display:none;} /* Hide all answers */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faq-question">
  <h3>Question 1</h3>
  <div>answer 1...</div>
  <h3>Question 2</h3>
  <div>answer 2...</div>
  <h3>Question 3</h3>
  <div>answer 3...</div>
</div>

Answer №3

Check out the slideToggle() function for a handy animation effect.

$('#clickme').click(function() {
  $('#book').slideToggle('slow', function() {
    // You can add custom code here.
  });
});

See an example here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason for Firefox cutting off circular PNG images at the bottom?

Can you explain why Firefox is cutting off circular png images at the bottom of this page: It's strange that IE 10 and Chrome don't have this issue with cropping the images. ...

Is there a limit to the number of else if statements I can use? The final else statement

How can I enhance this code snippet? The last else if statement is not working, despite my efforts to fix it. var selectedDntTyp = $("#donationTypDD"); if (selectedDntTyp.length > 0) var DropInitValue = selectedDntTyp.val(); if(DropInitValue == &apos ...

What is the best way to include a form within every row of an HTML datatables structure?

I have a regular table that is populated with data fetched via Ajax, and it appears like this: Ajax <script> $(document).ready(function() { $('#mytable').DataTable( { "ajax": "myurl", "dataType": 'json', ...

Is it possible to conceal any spans that are in close proximity to the cursor when hovered over?

Currently, I am working on a project that involves multiple spans placed side by side, with each span containing a letter of the text. My aim is to create a functionality where hovering over one of these spans will not only hide that particular span but al ...

The data type 'string' cannot be assigned to the data type 'Position'

Currently, I am in the process of converting React js to typescript. The component being used is a Class Component. I would like to obtain CSS settings through props and apply them to an element. How can I resolve this issue? render(){return( <span st ...

`Responsive Site with Centered Image Overlay`

I've been attempting to center a small image within a larger one, both of which are contained within a Bootstrap Column. <div className="row justify-content-center my-5 "> <div className="col-xs-10 col-sm-6 my-auto mx-auto" ...

Tips for restricting text within a div container

Currently, on my to-do list website, I am facing an issue with displaying long event titles in a div on the home screen. The text overflows, and even though I have set the x-overflow to hidden, it's not providing the desired result. I have tried using ...

Issues with functionality of Bootstrap carousel slider

Currently, I'm attempting to put together a Bootstrap carousel slider with indicators by referencing the Bootstrap code. The caption setup is functioning as expected, but I'm encountering issues with the slider itself. The problems I'm faci ...

Each container has its own div counter

Help needed to complete this code. The task is to count the number of .resp-containers in each container separately. Then, based on that count, assign a corresponding class to each element within the containers. Check out the code here $(document).ready(f ...

`Positioning of inline-block elements`

I am encountering an issue with the display CSS attributes set for three tags: a - inline-block img - block span - inline <a id="first"> <img/> <span> A first line of text B second line of text </span> </a> <a id="secon ...

Can the minimum length be automatically filled between two elements?

I'm struggling to find a way to adjust the spacing of the "auto filling in" dots to ensure a minimum length. Sometimes, on smaller screens, there are only one or two dots visible between items. Is there a way to set a minimum length for the dots in th ...

Interactive Map Displayed within a Pop-up Window

Recently, I developed a custom Google map where points are plotted and an HTML popup window appears when the image is clicked. Now, my goal is to open a file with JavaScript functions inside a lightbox/fancybox when a user clicks on an image. Below is th ...

Fully responsive header designed for optimal experience at any screen height

I am facing issues with the header and cannot seem to find a solution. My goal is to make the header span 100% of the window screen height. I need a responsive header that adjusts to 100% height, and when resizing for a smaller viewport, nothing should sho ...

Ways to ensure that the dropdown content remains visible even after the mouse has exited the trigger element

There are three buttons arranged in a row, and when one is hovered over, dropdown content appears underneath all three buttons. However, the content disappears when the mouse moves down to it. Unfortunately, images cannot be posted inside yet** https://i.s ...

The value of `$(this).data('dynamic_id')` is only defined within the `alert` function

Can someone please provide guidance on how to pass dynamically generated IDs from a Rails app to jQuery using the data-attribute? I have encountered an issue where the value returns as undefined unless it is sent to an alert box. I am currently using this ...

Challenges when working with AJAX/jQuery in terms of fetching JSON data and setting dataType

I am currently facing a challenge with my practice of AJAX/jQuery coding. Despite my efforts to learn and improve, I find the concepts of jQuery and AJAX quite perplexing. Specifically, I am struggling to understand dataTypes and how to manage different ty ...

Styling with CSS based on the remaining width of the viewport relative to the height

If you're viewing this on a tablet browser, the dimensions are set relative to the viewport width and height. Let's assume a landscape orientation for simplicity. Inside a fullscreen container, there are two divs positioned absolutely, just like ...

Two miniature tables placed next to each other within a single cell, both sharing equal height

Is there a way to make two tables within a parent table cell have the same height using HTML and CSS? If one cell has more content than the other, is it possible to adjust the height of both tables accordingly? <table cellpadding="0" border="1" cell ...

Troubleshooting Vue 3 Options API custom element with non-functional Bootstrap js files

Having trouble incorporating Bootstrap 5 into a Vue 3 Options Api custom element? Check out my setup below: import { defineCustomElement } from 'vue' import 'bootstrap/dist/js/bootstrap.bundle' import App from './App.ce.vue' ...

What could be the reason for Jquery post not successfully transmitting data?

So, I've encountered a very strange issue. I'm creating an object and sending it to the server, but for some reason, the data is not coming through (the JSP getParameter function is returning null). var _formData = { "u_id": user.userId, ...