Make Divi Accordions Closed by Default

The default behavior of the Divi Theme's accordion module is to show the first item of the accordion as open. If you'd like to have all accordion items to start in the closed state by default, you can do so by adding the following jQuery code to Divi:

<script>
jQuery(function($){
    $('.et_pb_accordion .et_pb_toggle_open').addClass('et_pb_toggle_close').removeClass('et_pb_toggle_open');

    $('.et_pb_accordion .et_pb_toggle').click(function() {
      $this = $(this);
      setTimeout(function(){
         $this.closest('.et_pb_accordion').removeClass('et_pb_accordion_toggling');
      },700);
    });
});
</script>

Changing the Accordion Initial State with Divi Booster

Divi Booster adds an option to set the initial state of individual accordions into the Accordion Module settings. You'll find it at:

  • Divi Booster 2.9.5 upwards: "Accordion Module Settings > Design > Toggle > Initial State"
  • Divi Booster 2.8.4 – 2.9.4: "Accordion Module Settings > Design > Title Text > Initial State"

You can set the initial state of individual accordions to one of "Default", "All Closed" and "All Open".

It is also possible to make all accordions on the site start closed using the older option found under "Modules > Accordion" on the Divi Booster settings page.

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

126 Comments

  1. Hi,

    Before I go add this (exactly what I need), please clarify where exactly I should put the code. I understand that I need to call it from the accordion advanced tab, but am not clear on where to put the code itself. Thanks!

    Reply
  2. Hello, I am using the Divi booster to start with a closed Accordion. It works great on all the websites except for one: It starts open and will not even close. Everything is up to date. Any ideas on how to get it to work and what the problem is? Please look towards to bottom of this website for the accordion just above the Sacramento map: http://thejunkrevolution.com

    Alex

    Reply
    • Hi Alexandra,

      I think the issue is that you have a JavaScript error on that page. ​The error is in a separate plugin (WP Review Slider Pro), but can have the knock-on effect of preventing the JavaScript in other plugins such as Divi Booster from being able to run (Divi Booster uses JavaScript to add the accordion functionality).

      ​You should be able to confirm whether this is what's causing the issue by temporarily disabling WP Review Slider Pro – if it was the problem the accordion should start working correctly. You may need to contact the developers of that plugin to figure out why it is giving you an error, but it looks like one of its files (wprs-unslider.min.js) is not being found by the browser.

      ​I hope that helps, but let me know if you still can't get the accordion to work after that.

      Reply
  3. Very good work! Thanks

    Reply
  4. Great!
    Thank you very much.

    Reply
    • Hi irfan, please ask away :)

      Reply
  5. Thanks, Dan! This is great. I used 3 of your posts to work out how to close the accordion, start it out closed and add the jquery code to Divi so I can use it as a class! Learning everyday :-)

    Reply
    • You're welcome, Sharon! With this and the upload path stuff we were discussing, you've definitely been getting stuck in this year :)

      Reply
  6. I added the code as suggested and it is closed on page load but I am unable to close it once opened…is that how it's supposed to work? Do I need to add something else?

    Reply
    • Hi Denise, that's how it's supposed to work. The code simply makes them closed to begin with, after which it reverts to the standard accordion behavior (i.e. of always having one open). I have a separate tip for making the accordions closeable:

      https://divibooster.com/make-divi-accordion-module-closable/

      The two tips will work together, giving you accordions which start closed and can subsequently be re-closed. Hope it helps!

      Reply
      • dan THANK YOU, it's working perfectly now! i appreciate the help so very much!

        Reply
      • These two solutions were exactly what I needed – thank you so much for posting them! Works like a charm!! :D

        Reply
      • Dan, thank you so much!

        Reply
  7. THANKS. This is perfect. Note that I simply added the code to a code module and it worked great. I didn't want to apply this to the entire site.

    Reply
    • How were you able to add it to an individual module? Did you add it to the Accordion Module itself or the Item? Did you place the code in the 'After' field, or 'Toggle' field…? Thanks!

      Reply
      • Hi Kate, if you only have one accordion on your page, you can just put this code into a code module on the same page. Otherwise, give your accordion module a CSS class (on the Advanced / Custom CSS tab of the module options), such as "my_accordion" then replace the three occurrences of ".et_pb_accordion" in the code above with ".my_accordion". Note that adding it to the "After" or "Toggle" field won't work as they are for CSS code, but the code in this post is jQuery code. Hope that helps.

        Reply
  8. Hi – I downloaded the Divi booster and checke off the Make accordions start fully closed by default and Make accordions closable. But nothing happens beside it now got the button to close but it does not work. I'm I supposed to also put the code in the footer?
    Kind regards
    Elisabeth

    Reply
    • Hi Elisabeth, with Divi Booster there is no need to add code into the footer – it adds the code it needs. It sounds to me like this might be a caching issue. Try clear the caches in any performance plugins you have installed to see if that helps. If not, please let me know.

      Reply
  9. Hello,

    I'm struggling to add a Jscript embedded form (Marketo).

    Divi is stripping out the tags, and I don't know if I need to add something to the code under the Integration tab.

    Code example:

    MktoForms2.loadForm("//app-ab12.marketo.com", "169-RVQ-430", 1279);

    Reply
    • Hi Salem, how are you adding the javascript – is this in a text module? If you haven't already, I'd try adding it using a "code" module. The code module is designed for exactly this type of thing, and shouldn't strip the tags.

      Reply
  10. Hi thank you for this example. I came across your page while searching how to change the heading level of toggle titles in Divi — it is set to H5 by default which limits its use and presents an accessibility issue. Any chance you can show us how to do that with javascript? I'd like to set it to H3.

    Thanks very much!

    Reply
    • Hi Rrezarta, I think the following should help. You can paste the whole lot into "Divi > Theme Options > Integrations > Add Code to the Head of your Blog". The CSS I've included just tidies things up a bit, but you may need to tweak it if you've got non-default styling for your toggles.

      <script>
      jQuery(function($){
      
      	var attrs = { };
      
      	$.each($("h5.et_pb_toggle_title")[0].attributes, function(idx, attr) {
      		attrs[attr.nodeName] = attr.nodeValue;
      	});
      
      	$("h5.et_pb_toggle_title").replaceWith(function () {
      		return $("<h3 />", attrs).append($(this).contents());
      	});
      });
      </script>
      
      <style>
      .et_pb_toggle_title { 
      	padding-bottom: 0; 
      }
      .et_pb_toggle_title:before { 
      	top: inherit !important; 
      	padding-top: 10px; 
      	right: 20px !important; 
      }
      </style>
      

      Hope that helps!

      Reply
      • Hi Dan,

        Just wanted to let you know that we tweaked your code to the one below to avoid some issues it was causing with our video embeds:

        jQuery(function($){

        var attrs = { };
        if ( $("h5.et_pb_toggle_title").length ) {
        $.each($("h5.et_pb_toggle_title")[0].attributes, function(idx, attr) {
        attrs[attr.nodeName] = attr.nodeValue;
        });
        $("h5.et_pb_toggle_title").replaceWith(function () {
        return $("", attrs).append($(this).contents());
        });

        }

        Reply
        • Cool. Thanks for sharing that, Rrezarta :)

          Reply
  11. This is great. Thank you. I'd

    Is there any way to limit how many can be open at a time?
    Can be able to close an accordion without opening another one? Right now it only closes if you open another one. I'd like to close it straight away.

    Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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