Search code examples
javascriptaccordionjquery-ui-accordion

Trying to hack accordian script so they are closed by default


So, I have a theme with an accordian option in it - however the accordian opens the first item. I would like it to be closed - I know it's controlled with JS which is not my forte.

I can see the JS adds 'open' and 'active' classes into the area but I can't seem to see how it works out the first one.

Here is the code.

function handleAccordionBox(){
    $('[data-norebro-accordion]').each(function(){
        var accordion = $(this);
        var titles = $(this).find('div.title');
        var items = $(this).find('.item');
        var contents = $(this).find('.content');
        var iconOpened = 'ion-minus', iconClosed = 'ion-plus';
        var isOutline = $(this).hasClass('outline');

        var toggle = function( num ){
            var opened = accordion.find('.open');
            var content = contents.eq( num );

            // If not active
            if( !items.eq( num ).hasClass('active') ){
                // Activate this item
                items.removeClass('active');
                items.eq( num ).addClass('active');

                setTimeout(function(){
                    content.css('height', '').addClass('no-transition open');           // Open new content
                    var height = content.outerHeight() + 'px';                             // Save heights
                    content.removeClass('no-transition open').css( 'height', (isOutline) ? '0px' : '6px' ); // Close new content

                    setTimeout(function(){
                        opened.removeClass('open no-transition').css( 'height', (isOutline) ? '0px' : '6px' ); // Close old content
                        content.addClass('open').css( 'height', height );               // Open new content

                        // Change titles
                        titles.find('.control span').removeClass( iconOpened ).addClass( iconClosed );
                        titles.eq(num).find('.control span').removeClass( iconClosed ).addClass( iconOpened );
                    }, 30);
                }, 30);
            }
            else  {
                items.removeClass('active');
                opened.removeClass('open no-transition').css( 'height', (isOutline) ? '0px' : '6px' );
                titles.find('.control span').removeClass( iconOpened ).addClass( iconClosed );
            };
        };

        titles.each(function(i){
            $(this).on('click', function(){
                toggle( i );
            });
        });

        toggle( $(this).attr('data-norebro-accordion') || 0 );

        this.accordionToggle = toggle;
    });
};
function handleAccordionBoxSize(){
    $('[data-norebro-accordion]').each(function(){
        var content = $(this).find('.content.open');
        var wrap = content.find('.wrap');

        content.css('height', wrap.outerHeight() + 'px');

    });
};`

Solution

  • The key line is this one:

    toggle( $(this).attr('data-norebro-accordion') || 0 );
    

    toggle is the function that actually opens and closes the menu. Once everything has been declared, it's called once here, for either the element with the data-norebro-accordion attribute, or, if that's not declared, the first element (0)

    If you want everything closed by default, then just comment that line:

    // toggle( $(this).attr('data-norebro-accordion') || 0 );