Subscribe On YouTube

Join other subscribers and enjoy other Divi video tutorials!

How To Set The Toggle Or Accordion Module Default State Open Or Closed

Nelson Miller Profile Orange
In this tutorial I will show you how to set the default state of the Divi Accordion module open or closed when the page loads.

▶️ Please watch the video above to get all the exciting details! 👆

Set The Divi Toggle Module State

The Divi Toggle module can easily be set to open or closed with a setting. This setting is easy to find in the Toggle settings>Content tab>State toggle. Just select either Open or Close as your desired option, and this is how it will show when the page loads.

set the default state of the Divi Toggle module

Set The Divi Accordion Module State

If only it was this easy for the Divi accordion module as it is for the Divi Toggle module. There is currently no setting for this in the Accordion module. The worst part is that the first accordion item is actually open by default. 

the Divi Accordion module shows the first item open by default on page load

In my opinion, this is not good, it looks especially bad if you have several columns or sections of accordions. So the only solution is to use jQuery. This is a difficult problem to solve, and many other Divi bloggers have tried and failed. So we came up with this solution that works great, but it does require a larger snippet of code than normal.

1. Add The CSS Class To The Accordion Module

The first step is to give the Accordion module a custom class. This is used so that we can target this specific Accordion module with the jQuery code. Go to the Accordion module settings to the Advanced tab and open the CSS ID & Classes toggle and place the custom class “pa-accordion” into the CSS Class input field.

add a CSS class for setting the default Divi accordion module state to closed

2. Add The jQuery Snippet To Your Website

The last step is to add the jQuery code snippet to your Divi website that will create the functionality of closing the Divi Accordion module by default.

Where To Paste The jQuery Code

1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the jQuery tab in the custom code window in the Divi Visual Builder.

2. Child Theme
If you are using a child theme, paste this code into the scripts.js file (don't forget to remove the <script> tags at the beginning and end). If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.

3. Divi Theme Options Integration
Otherwise, paste this code in your Divi>Theme Options>Integrations tab in the "Add code to the < head > of your blog" code area.

If you need help understanding where to paste the code, please check out our complete guide about where to add custom code In Divi.

<script>
jQuery(document).ready(function(){
    var setState = "close";
    if(setState == "open"){
        jQuery(".pa-accordion .et_pb_toggle").removeClass("et_pb_toggle_close");
        jQuery(".pa-accordion .et_pb_toggle").addClass("et_pb_toggle_open");
        jQuery(".et_pb_toggle_open .et_pb_toggle_content").css("display", "block");
        jQuery(".et_pb_toggle_open .et_pb_toggle_title").addClass("all_on");
        jQuery(".all_on").on("click", function(){
            
            
            if(jQuery(this).parent().hasClass("et_pb_toggle_open")){
                jQuery(this).parent().removeClass("et_pb_toggle_open");
                jQuery(this).parent().removeClass("et_pb_toggle_close");
                jQuery(this).parent().css("padding", "20px");
                jQuery(this).parent().siblings().removeClass("et_pb_toggle_close");
                jQuery(this).parent().siblings().removeClass("et_pb_toggle_open");
                jQuery(this).parent().siblings().css("padding", "20px");
              	
            }
          jQuery(this).removeClass("all_on");
        
            
        })
        
    }else{
        jQuery(".pa-accordion .et_pb_toggle").removeClass("et_pb_toggle_open");
        jQuery(".pa-accordion .et_pb_toggle").addClass("et_pb_toggle_close");
        jQuery(".et_pb_toggle_close .et_pb_toggle_content").css("display", "none");
    }
})
</script>

In the above code, there is a variable called setState which is set to “close” in our snippet. You can change this text to “open” if you want. If the value is set to “close” then the code will close all the accordions on page load. If the value is set to “open” then the code will open all the accordions on page load.

Subscribe For More Things Like This!

At the start of each month, we send out a recap newsletter from the month before with family news, Divi news, our latest tutorials, and product news. Occasionally, if the news is too exciting to wait, we will send out another email separate from the monthly newsletter. That’s what you get when you subscribe, and of course you can unsubscribe if you are no longer interested!

Blog Post Optin
Divi Logo (2)

Shop Our Divi Products

Plugins • Courses • Templates

Visit The Shop

Featured Products

Asset 4

New! Trail Guides

Follow a series of blog posts carefully arranged around a specific topic or goal! Keep track of your progress by marking posts completed, just like a free course!

View Trial Guides

Divi Tutorials On YouTube

Our videos have views! Join subscribers and enjoy over video tutorials!

Visit Our Channel

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

23 Comments

Comments By Members

These comments are highlighted because they were posted by fans who have a membership on this site.

  1. Doren Michael <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

    Hi, how in the world do you auto-close an Accordian when the Accordians are all in different rows & columns? They have to be designed like this, but the auto-close doesn’t work when a new Accordion is opened in different rows & columns. Thanks!

    Reply
  2. John Sharkey <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

    ANother hack to do this is to add a first accordion item, then it its advanced tab under main element add display: none;

    Reply
  3. Carol Tomalty <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

    This is the information that I need, but when I look at the Accordion Settings in the module, there is no ‘State’ – https://tinyurl.com/2bveusy7. Did they remove that option or am I missing something? I’m using Divi 4.22.2. I seem to recall that I always had to use a work-around, but I hopes that wasn’t the case when I saw your article.

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Carol!

      It seems you’re using the accordion module. The State option is in the Toggle module. To change the Accordion module, please use the script provided in the guide above.

      Reply
    • Andi

      same to me, i also have no Toggle Stat in my Divi (newest Version 4.23)

      Reply

Comments By Others

  1. Yoli

    OMG thank you! Seems so silly that this isn’t in the module by default, I spent at least an hour trying to figure this out with ChatGPT.

    Your solution was implemented in like 4 minutes. Thank yoU!

    Reply
  2. Rick Elrod

    Do you know how to have the according close when you click on it again?

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Rick!

      We will consider the request and apply it to the customization in the future.

      Reply
  3. Ivo

    Hi. Script works great, but once I open my accordion, it won’t close again. Am I missing something, or can it not do that?

    Reply
  4. Yen

    Hi Nelson!
    Thanks for the tutorial.
    Is there any way that I can open a specific Accordion by default?
    Thanks in advance!

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hi Matt!

      I’m not able to see the script in the page source code. Did you remove it?

      Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hey Ammie,

      I have checked the URL you provided and as I could see, your website is developed on Elementor and this snippet is for the Divi-developed websites.

      Reply
  5. Renaud Destermes

    Thank for this tutorial. I don’t know why, but it dosen’t work on my website, coul’d you have a quick look please (in the ham menu)? When one is open, second donc close the first… Thanks in advance

    Reply
    • Hemant Gaba <span class="comment-author-role-label"><a href="https://www.peeayecreative.com/product/divi-adventure-club/" class="comment-author-role-link" rel="external nofollow" target="_blank">Divi Adventure Club Member</a></span>

      Hey Renaud,

      Could you please share the URL of the page where the accordion is present and please let me know what state you want to set to default?

      Reply

Submit a Comment

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

Recent Posts

0

Your Cart