Tips for shutting down an open section within a jQuery Accordion?

I am having trouble closing the active panel using jQuery. The rest of my code is functioning perfectly, but for some reason, I can't get the active panel to close.

Currently, the code is working correctly in that only one panel can be open at a time, which is exactly what I want. I'm sure the solution to fixing the issue is simple!

$(document).ready(function($) {

 
   $( "#accordion").on('click','.accordion-heading', function (e) {
            $return = true;
            if($(this).next('div').is(':visible')== false) $return = false;
            $( "#accordion .panel2").slideUp();
            $("#accordion  .accordion-heading").removeClass('active');
            $(this).next('div').stop(true,false).slideDown();
            $(this).addClass('active');
            return $return;
        });
.panel1 {
  width: 60%;
  margin-left: 20px;
}

.accordion-default {
  border-bottom: 1px solid #C8C8C8;
}

/* CSS styles omitted for brevity */

</script>
<div class="panel1" id="accordion">
  <div class="accordion-default">
    <div class="accordion-heading">​
      <a class="accordion-toggle collapsed" >Heading1</a> </div>
    <div class="panel2 accordioncollapse collapse" ">
      <div class="accordion-body"> Collapsible Content #1 </div>
    </div>
  </div>
  <div class="accordion-default">
    <div class="accordion-heading">
      <a class="accordion-toggle collapsed"  >Heading2</a> </div>
    <div class="panel2 accordioncollapse collapse" >
      <div class="accordion-body"> Collapsible Content #2 </div>
    </div>
  </div>
  <div class="accordion-default">
    <div class="accordion-heading">
      <a class="accordion-toggle collapsed"  >Heading3</a> </div>
    <div class="panel2 accordioncollapse collapse" id="collapseThree" >
      <div class="accordion-body"> Collapsible Content #3 </div>
    </div>
  </div>
</div>

Answer №1

It is important to verify if the current panel contains the "active" class. If it does, simply toggle it and close the panel. Otherwise, proceed with your usual actions.

Take a look at this illustration:

$(document).ready(function($) {
   $("#expand").change(function() {
       if (this.checked) {
           $("#accordion  .accordion-heading").addClass('active locked');
           $('.panel2').slideDown();
       } else {
           $("#accordion  .accordion-heading").removeClass('active locked');
           $('.panel2').slideUp();
       }
   });
   $("#accordion").on('click','.accordion-heading', function (e) {
      if (!$("#expand").prop("checked")) {
          if ($(this).hasClass('active')) {
              $(this).next('div').slideUp();
              $(this).removeClass('active');
          } else {
              $("#accordion .panel2").slideUp();
              $("#accordion  .accordion-heading").removeClass('active');
              $(this).next('div').stop(true,false).slideDown();
              $(this).addClass('active');
          }
      }
   });
});
.panel1 {
  width: 60%;
  margin-left: 20px;
}

.accordion-default {
  border-bottom: 1px solid #C8C8C8;
}

.accordion-heading>a {
  display: block;
  line-height: 1.875;
  font-size: 16px;
  font-style: normal;
  font-family: sans-serif;
  font-weight: bold;
  text-decoration: none;
  color: black;
  padding-top: 18px;
  padding-bottom: 18px;
}

.accordion-heading>a:hover,
.accordion-heading>a:active,
.accordion-heading>a:focus {
  text-decoration: none;
  color: #D0006F;
}

.accordion-heading.active .accordion-toggle.collapsed::before{
  opacity: 0;
}
.accordion-heading.active .accordion-toggle.collapsed::after {

  transform: rotate(-180deg);
}
.accordion-body {
  margin-left: 2px;
  padding-top: 5px;
  padding-bottom: 10px;
}

.accordion-toggle {
  padding-left: 2px;
  position: relative;
  cursor: pointer;
  text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

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

Navigation bar in reactjs remains visible even after clicking on an icon button, causing a temporary issue with

I'm facing an issue with my navigation bar created using material-ui. I have an onClick function set up so that when the user clicks the icon button, the navigation redirects to a new page and then should close. However, no matter what I try, the navi ...

What is the best way to extract the data parameter from an Ajax request in a Restful web service?

When making an AJAX call to a Restful web service, I encounter a problem where the data parameter passed is null. Here is the code snippet: var b2bValues = "Some String"; var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest"; var l ...

Replicate a click using jQuery to retrieve a filtered multigallery based on the URL

I have a website that was built using Elementor for a photographer. The site features an Elementor Multigallery with a filter at the top. I am looking to create external links that will direct users to specific subpages showing only filtered items. For ex ...

Could there be any issues with the structure of my mongoose schema?

I've been stuck for 3 hours trying to solve this problem. I can't seem to retrieve any data from my document. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var accountSchema = mongoose.Schema({ username: String ...

Issue with Bootstrap 4 navigation tabs: dropdown items do not cycle through active tab panels

Whenever I utilize nav-tabs to toggle between tabpanels, it functions properly. However, there seems to be an issue when attempting to switch the active tabpanel using the links in the dropdown menu. Below is the HTML for my nav-tabs and dropdown menu, alo ...

Combining HTML file input with a form submission using a button of type "button", along with jQuery in the context of MVC 2.0 in Asp.net

I have a requirement in MVC 2.0 and ASP.NET where I need to upload multiple files using a partial user control view without submitting the form using an input button. The challenge is to post the form with only a button type of 'button' so that ...

"Experience the magic of Datepicker jQuery with just a click

Does anyone know how to capture the click event when a day is selected using jQuery Datepicker? Is it possible, and if so, how can I achieve this? https://i.sstatic.net/ZG4r8.png Here is the HTML code snippet: Date From : <input type="text" ...

The suggestions are not being displayed by jquery-typeahead

I have integrated jquery-typeahead into my Angular application. Here is the HTML: <input class="js-typeahead-locations_v1" name="locations_v1[query]" type="search" placeholder="Search here" autocomplete="off"> The controller code snippet is as fol ...

What is the best way to delete a specific date from local storage using Angular after saving it with a key

I'm attempting to clear the fields in my object (collectionFilter) from local storage using localStorage.removeItem('collectionFilter'). All fields are removed, except for the date fields. Please note that I am new to JavaScript and Angular. ...

Creating Browser Extensions with Vue.js and Vue CLI

I am in the process of creating a Chrome Extension with a frontend powered by Vue.js. Everything was going smoothly using vuecli until my app started utilizing the Webextension-API. This API is only accessible to registered Extensions, not normal websites. ...

Experiencing problems with the Datepicker and cloning functionality in JQuery?

This is the section of code responsible for cloning in JavaScript. $(document).on("click", ".add_income", function () { $('.incomes:last').clone(true).insertAfter('.incomes:last'); $(".incomes:last").find(".income_date_containe ...

Apologies: the declaration file for the VueJS application module could not be located

Hey there! I'm currently working on a VueJS application using NuxtJS. Recently, I added an image cropping library called vue-croppie to my project. Following the documentation, I imported the Vue component in the code snippet below: import VueCroppie ...

Separate a tab delimited text file and store it into an array using JavaScript

Looking to parse a text file with tab-separated values in JavaScript and add them to an array? Here's how you can achieve this while excluding the header: Id Symbol 1 A 2 B 3 C 4 D 5 E 6 F 7 G This is what I have attempted so far: va ...

Angular material table featuring custom row design

My team is working with a large table that is sorted by date, and we are looking to add some guidance rows to help users navigate through the data more easily. The desired structure of the table is as follows: |_Header1_|_Header2_| | 25/11/2018 | ...

The issue of duplicate backgrounds appearing on the burger menu when adjusting the height

There seems to be an issue with my burgermenu; when I attempt to adjust the height, the background-color ends up duplicating. .bm-menu { background-color: rgba(255, 255, 255, 0.9); height: 60vh; position: fixed; transition: top 0.3s; to ...

What is the process for constructing an object to resemble another object?

After collecting input data, I have created an object based on those values. Here is an example of the generated object: var generate_fields = { name: "Mike", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d9dddf ...

Is there a way to incorporate a callback function for animation in cgscenegraph?

After creating my animation, I am interested in incorporating a callback function when the loop of the animation comes to an end. Is there a way you can guide me on achieving this? Thank you. ...

Ways to locate two div class elements that are generated dynamically

I am looking to dynamically create 2 divs in different locations. One for displaying information and another for editing information. I want to be able to delete both divs with the same class when using the delete button. Since they are located in differe ...

Is Redux really the new trend in the world of action creators?

I am new to this. I'm a bit unsure, is it okay or silly to use the pattern below? import { createAction, handleActions } from "redux-actions"; const CHANGE_STATE = "appState/CHANGE_STATE"; export const changeState = createAction(CHANGE_STATE, (key, ...

exploring various dynamic elements using jquery

If you need some help figuring this out, take a look at the JSFiddle here. I've set it up for users to input any data they want in the text box, choose a table from set one, and then click the "submit" button to send it. <div> <Div> < ...