ReactJS how to prevent accordion from collapsing automatically

My custom accordion layout for the features needed in the site I am building is not working well with Jquery. How can I modify it to collapse properly? Specifically, I want it so that when I open number 2, number 1 will automatically close.

I've tried using the common jquery methods found online, but they don't seem to be effective. I've been staring at my screen with a puzzled look for an hour now.

<div id="accordion">
<div className="GreyCircle top" />
<div className="Item" >
    <div className="DestinationMarkerGreen active" >1</div>
    <div className="square" />
    <div className="Content" >
        <div className="card">
            <div className="card-header" id="heading1" data-toggle="collapse" data-target="#collapse1" aria-expanded="true" aria-controls="collapse1">
                <h4>Bristol</h4>
                <p>England, United Kingdom</p>
            </div>
            <div id="collapse1" className="collapse show in accordionItem" aria-labelledby="heading1" data-parent="#accordion">
                <div className="card-body">
                              Include your code here.
                            </div>
            </div>
        </div>
    </div>
</div>
<div className="Item" >
    <div className="DestinationMarkerGreen" >2</div>
    <div className="square" />
    <div className="Content" >
        <div className="card">
            <div className="card-header" id="heading2" data-toggle="collapse" data-target="#collapse2" aria-expanded="true" aria-controls="collapse2">
                <h4>Bristol</h4>
                <p>England, United Kingdom</p>
            </div>
            <div id="collapse2" className="collapse  accordionItem" aria-labelledby="heading2" data-parent="#accordion">
                <div className="card-body">
                              Include your code here.
                            </div>
            </div>
        </div>
    </div>
</div>
<div className="Item" >
    <div className="DestinationMarkerGreen" >3</div>
    <div className="square" />
    <div className="Content" >
        <div className="card">
            <div className="card-header" id="heading3" data-toggle="collapse" data-target="#collapse3" aria-expanded="true" aria-controls="collapse3">
                <h4>Bristol</h4>
                <p>England, United Kingdom</p>
            </div>
            <div id="collapse3" className="collapse show accordionItem" aria-labelledby="heading3" data-parent="#accordion">
                <div className="card-body">
                              Include your code here.
                            </div>
            </div>
        </div>
    </div>
</div>

Here is what the image should look like

Answer №1

$('#accordion').on('show.bs.collapse', function() {
  $('#accordion .in').collapse('hide');
});

This solution resolved the problem I was facing.

Answer №2

Have you considered using the jQuery UI Accordion plugin available at https://jqueryui.com/accordion/? If you're unfamiliar with jQuery, it's best to utilize tools like this rather than attempting to build your own solution from scratch. :)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Accordion - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#accordion" ).accordion();
  } );
  </script>
</head>
<body>

<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit...
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
    Duis auctor ultricies mauris, vel varius ligula tempus non.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
    Quisque efficitur felis magna, nec volutpat justo blandit at...
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
    Curabitur fermentum enim vitae leo congue mattis.
    </p>
    <p>
    Vivamus accumsan odio eu purus molestie dapibus.
    </p>
  </div>
</div>

</body>
</html>

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

The specified file ngx-extended-pdf-viewer/assets/pdf.js cannot be found

I have integrated the ngx-extended-pdf-viewer package in my Angular application using npm to enable the display of PDF content. According to the setup instructions, I have added the following configuration in my angular.json file: "assets": [ ...

Do these exports serve the same purpose?

Can someone help me understand why one export works while the other doesn't? I've tried to figure it out on my own but I'm stuck. Functioning Example const dataStore = new Vapi({ baseURL: 'http://domain.test/api', state: ...

Showcasing Information Using AngularJS from a Json Array

My goal is to present data from a JSON array in a table using the code below. However, all the data is currently being shown in one row instead of each record appearing on separate rows with alternating colors - grey for even rows and white for odd rows. I ...

Tips for managing responsive font sizes as screen size decreases

Hey there, I could really use some assistance with a new website I've been working on. While I have experience building Joomla sites for the past 6 months, this is my first attempt at making a responsive site using media queries. You can check out th ...

Is it time to ditch daylight saving time - am I managing my dates and times correctly?

Currently, I am in the process of working on a PHP/mySQL web application where dates are stored as unix timestamps in columns that are UNSIGNED INT(10). When it comes to displaying these dates in the web view, we utilize moment.js to parse the numbers. On ...

Assign a value to an element based on a specific attribute value

I'm working with the following element <input type="text" size="4" name="nightly" value="-1"> My goal is to update the value to 15.9 specifically when name="nightly" Here's what I've attempted so far: document.getElementsByName(&ap ...

Passing a pair of variables through an onclick event back to the original page

Is there a way for me to click on a list element and have it display 2 variables on the same page depending on which one I click? Currently, I have two variables defined in the HTML code (although this could be changed as I am hardcoding them into the HTM ...

Design the header and body of the table in a visually appealing format

$("input:checkbox:not(:checked)").each(function() { var column = "table ." + $(this).attr("name"); $(column).hide(); }); $("input:checkbox").click(function() { var column = "table ." + $(this).attr("name"); $(column).toggle(); }) <script src="h ...

What is the method for retrieving an array or object that contains a collection of files designated for uploading within the jQuery file upload plugin?

Currently, I have successfully integrated a form into my Rails site and set up the jQuery file upload plugin. The upload form functions properly with the ability to select multiple files and utilize all ajax upload features. However, a challenge I am faci ...

How can I align the text in a dropdown menu to the center on Safari?

How can I center the option text in a drop down menu using CSS? display: block; height: 60px; margin-left: 10%; margin-right: 10%; margin-top: 10px; min-height: 60px; text-align: center; The option text is centered in Firefox browser, but not in Safari. ...

What is the procedure for submitting all checkbox values through Google Apps Script?

My web app created using Google Apps Script can generate a custom table of data for users to select for calculations. I use Google Sheets to populate the table with values and checkboxes, but note that the table size may vary depending on the user. <fo ...

Unable to reinitialize the DataTable using Angular Datatable

I've been working on an Angular application that has a simple CRUD functionality. Initially, I tested my data with a static HTML table and everything was functioning as expected. However, I decided to implement a data table framework called Angular da ...

The custom div slider speed is too quick, specifically when transitioning back from a different browser tab

While observing the code in action, everything seems to work fine but when I switch to another browser tab and come back, the speed of the animation is too fast. Below is the jQuery code snippet: var divId = 1; var lp = 0; $(document).ready(function (){ ...

Submitting a Laravel form seamlessly without triggering a page refresh

In my Laravel 5.7 Project, I have a dilemma that needs resolving: Within my system, there is a table displaying all users with a column labeled Status, indicating whether they are Active or Inactive... Currently, in my Blade template, there is a select o ...

Troubleshooting CSS Code Issues on Myspace

Greetings! I am currently working on enhancing a friend's profile on myspace.com. I have two divs on each side of the screen that I want to align next to each other. I have tried using float: left; and float: right;, as well as experimenting with marg ...

Rendering HTML5 and CSS3 on Internet Explorer 8

My coding knowledge is limited, but I am conducting research for our front-end developers. Our goal is to revamp our portal application using CSS 3 and HTML 5 in order to achieve an adaptive layout that can accommodate different browser widths. The curre ...

How can we solve the issue of missing props validation for the Component and pageProps in _app.js?

Below is the code snippet from _app.js for a nextjs project that uses typescript import React from 'react' import '../styles/globals.css' function MyApp({ Component, pageProps }) { return <Component {...pageProps} /> } export ...

Incorporate an Ajax request onto an existing link within a div element

Here is what I have: <div id="div-for-ajax-onclick"> <a href="http://www.google.com">Link to google</a> </div> $("#div-for-ajax-onclick").click(function() { // update database }); I am looking for a solution where if someone c ...

What is the method for creating a background similar to that found on the Bootstrap homepage?

body>div { background-color: red; } <div>Additional content here</div> Looking for assistance in achieving a similar background to the one on the bootstrap main page. I attempted using a gradient, but was unable to achieve the desired re ...

Show dynamic outcomes on the site

I'm currently in the process of building a website for a travel search engine and I'm interested in displaying the results in a similar manner to what is done on Hipmunk. Here are some key details: The website is being developed using Django. ...