Guide to ensuring only one Accordion opens at a time in Elementor

I created a custom accordion in elementor with CSS and JavaScript, but I'm facing an issue where the tabs stay open when others are opened. How can I modify the code to make sure only one tab is open at a time?

Below is the CSS code snippet:

    body:not(.elementor-editor-active) .toggle-content{
        display: none;
    }
    
    .toggle-trigger {
        cursor: pointer;
    }
    
    .arrow i{
        transition-duration: 0.5s;
    }
    
    .tab_active .arrow i{
        transition-property: transform;
        -ms-transform:rotate(45deg) !important; /* IE 9*/
        transform: rotate(45deg) !important;
        transition-duration: 0.5s;
    }

And here is the corresponding JavaScript code:

jQuery(document).ready(function($){
        jQuery(".toggle-trigger").on("click", function(){
            let $this = $(this);
            
            if($this.hasClass('tab_active')){
                
                $this.removeClass('tab_active');
                $this.next(".toggle-content").slideUp(200);
                
            } else {
                
                $this.toggleClass('tab_active');
                $this.next(".toggle-content").slideDown(200)
                
            }
        })
    });

The class .toggle-trigger is for the title-tab section while .toggle-content is for the content that slides down on click. The .arrow class is responsible for changing the icon orientation upon clicking.

In addition, I'm experiencing a slight movement of the tab position when the accordion opens. How can I fix the tab position while only allowing the content to slide down?

I've attempted using addClass instead of toggleClass and removing the tab active class with jQuery('.toggle-trigger).removeClass('tab-active') without success. I also tried removing classes from other tabs using not($this), following suggestions on StackOverflow. As a JavaScript beginner, I would greatly appreciate any guidance on this matter.

Answer №1

Give this a shot

 jQuery(".toggle-trigger").on("click", function(){
     let $currentTab = $(this);

     const $activeTab = jQuery(".toggle-trigger.tab_active");

     $activeTab.next(".toggle-content").slideUp(200);
     $activeTab.removeClass('tab_active');
        
     $currentTab.addClass('tab_active');
     $currentTab.next(".toggle-content").slideDown(200)
});

To switch tabs, make sure to remove the active class from the current tab before adding it to the new one.

Answer №2

One potential solution is to close all open tabs before opening the selected tab.

jQuery(document).ready(function($){
    jQuery(".toggle-trigger").on("click", function(){
        let $this = $(this);

        // Close all open tabs except for the one that is clicked
        $(".toggle-trigger").not($this).removeClass('tab_active');
        $(".toggle-content").not($this.next(".toggle-content")).slideUp(200);

        // Toggle the clicked tab
        if($this.hasClass('tab_active')){
            $this.removeClass('tab_active');
            $this.next(".toggle-content").slideUp(200);
        } else {
            $this.addClass('tab_active');
            $this.next(".toggle-content").slideDown(200);
        }
    });
});

In case you are facing an issue where the accordion opens and shifts the position of the tab slightly upwards while the content slides down, a possible fix would be to set the position of the tab as fixed so only the content moves.

To achieve this, consider adding the CSS position: relative to the parent element of the tab. By doing so, the position of the tab will remain fixed relative to its parent while the content smoothly slides down beneath it.

.parent element {
   position: relative;
}

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

Dismiss the Popover in Ionic 2

After opening a popover that redirects me to another page and then returning to the root page (popToRoot), I reload the data/dom upon an event and dismiss the popup once the json data is received from the server. Everything works smoothly with a lengthy ti ...

`Warning: The alert function is not working properly in the console error

I am currently working on integrating otp functionality into my Ionic 3 project. I am facing an issue where I am able to receive the otp, but it is not redirecting to the otp receive page due to a specific error. Below is the console error that I am encou ...

Add a small symbol beside the hyperlink

I have a requirement where I need to display a back icon for the Previous button and a next icon for the Next button. The icons are currently being placed using Font Awesome's CDN. The code snippet I am using is as shown below: $('span.CustomerD ...

Creating a CSS layout that spans a single element across multiple divs

As a newcomer to the world of Html & CSS, I have a seemingly simple question that has been eluding me: How can you make an html element(child div, text, etc.) span across multiple divs using only CSS & Html(with no Javascript/JS)? Currently, I am working o ...

framer.motion animation happens immediately instead of gradually animating

I have a collection of boxes that I would like to animate fading in and out one at a time. Here's a basic example: Take a look at this simple app example on CodeSandbox. In the current setup, the box animation is instantaneous. I want each box to fa ...

Displaying without yielding

I am creating a simple BMI calculator that displays real-time results without the need to submit the form. Instead of submitting, I want a way to instantly show the calculated values and a message in a separate div. <label>Weight in kg <input ...

What is the best way to incorporate a N/A button into the dateRangeFilter located in the header of a webix dataTable, enabling the exclusion of N/A values within that specific column?

`webix.ready(function(){ grid = webix.ui({ container:"tracker", editaction:"click", editable:true, view:"datatable", css:"webix_header_border", leftSplit:1, ...

How can you append an object with a defined key to an array in Vue?

Currently developing a vue-application that includes a component for managing driving licenses. Here is an overview of my data setup: data() { return { custom_licenses: [], basic_licenses: [] } } Within my methods, I have the following l ...

Troubleshooting CodeIgniter's AJAX call functionality issue

I have been attempting to test AJAX functionality within CodeIgniter, but unfortunately, I haven't had any success so far. Can someone please point out where I might be making a mistake? Below is my test_page.php: <!DOCTYPE html> <head> ...

The current version of HTML5 Context Menus is now available

I'm in need of implementing the HTML5 Context Menu feature. Currently, only Firefox supports it. My main objective is to add some menu options without replacing the existing context menu. How can I achieve the same functionality now? I am aware of va ...

Analyzing JavaScript performance without causing your browser to crash

I recently experimented with profiling code and found that the most convenient method, at least on Firefox, was to utilize either console's time/timeEnd functions or profile/profileEnd methods, so I gave both a try. The issue I encountered was the li ...

Retrieving data synchronously from a Promise with Angular

I am looking to display the product history by retrieving the id of the product from the ActivatedRoute.params. In the ngOnInit method, I need to fetch all the historical records of the product and store them in a variable. Afterwards, I want to merge the ...

Is it possible to customize the deep elements of ExpansionPanelSummary using styled-components in React?

After digging into the documentation and examples on how to customize Material UI styling with styled-components, I successfully applied styling to the root and "deeper elements" within an ExpansionPanel and ExpansionPanelDetails. However, when attempting ...

Adjusting image size to fit divs depending on orientation

I am currently working on a project where I need images to fill a div while still maintaining their aspect ratio. To achieve this, I am utilizing jQuery to determine if the images are portrait or landscape, and adjusting the width or height accordingly. H ...

Is it feasible to automatically fill out a separate form by clicking a link?

Seeking help with creating an Android app that can automatically populate the "RFC Emisor" and "RFC Receptor" fields on a specific web page: https://verificacfdi.facturaelectronica.sat.gob.mx. I believe the IDs for these inputs are: ctl00_MainContent_ ...

Color remains unchanged for selected value in dropdown menu

I'm facing a challenge in changing the color of the selected value in the dropdown menu. Each value has a different color, but when I select one, it reverts back to black. .appt-status select option[value="0"] { color: #3EA47B; } .appt-status o ...

Launch an application directly from its package name using either Node.js or PHP

Is there a way to generate a unique link that contains the package name of an app, allowing for direct app launch if it is already installed on a mobile device? If the app is not installed, the user should be prompted to install it from the Play Store usin ...

The feature of securing HTML forms by user session

I have developed a website that enables the submission of HTML forms with multiple stages of authorization. For instance, an employee submits a leave request which is then approved by a supervisor. There can be multiple users with permission to authorize a ...

skip every nth element in the array based on the specified value

The Challenge I'm currently working on a graph that relies on an array of data points to construct itself. One major issue I've encountered is the need for the graph to be resizable, which leads to the necessity of removing certain data points ...

JavaScript Mouseover Custom Trigger with d3.js Library

Currently, I am experimenting with d3.js and am interested in creating a custom event with a unique trigger. From my understanding, the 'mouseover' event occurs when the mouse pointer hovers over a specific element both horizontally and vertical ...