Question

I have the following code for a jQuery expanding Div element:

$(document).ready(function()
{
//Add Inactive Class To All Accordion Headers
$('.accordion-header').toggleClass('inactive-header');

//Set The Accordion Content Width
var contentwidth = $('.accordion-header').width();
$('.accordion-content').css({'width' : contentwidth });

//Open The First Accordion Section When Page Loads
$('.accordion-header').first().toggleClass('active-header').toggleClass('inactive-header');
$('.accordion-content').first().slideDown().toggleClass('open-content');

// The Accordion Effect
$('.accordion-header').click(function () {
    if($(this).is('.inactive-header')) {
        $('.active-header').toggleClass('active-header').toggleClass('inactive-header').next().slideToggle().toggleClass('open-content');
        $(this).toggleClass('active-header').toggleClass('inactive-header');
        $(this).next().slideToggle().toggleClass('open-content');
    }

    else {
        $(this).toggleClass('active-header').toggleClass('inactive-header');
        $(this).next().slideToggle().toggleClass('open-content');
    }
});

return false;
});

This code only allows one div to be opened at a time. (When one is opened and I click on another, the first one closes.) How can I modify it so that I can open (expand) multiple divs at any given time.

Many Thanks

Was it helpful?

Solution

Look at my comments in code. If this doesnt work. Please make a fiddle so we can understand the problem better

$('.accordion-header').click(function () {
    if($(this).is('.inactive-header')) {
        // I think this line is the one thats toggling everything and closing 
        //all the open ones so try commenting it out
        //$('.active-header').toggleClass('active-header').toggleClass('inactive-header').next().slideToggle().toggleClass('open-content');
        $(this).toggleClass('active-header').toggleClass('inactive-header');
        $(this).next().slideToggle().toggleClass('open-content');
    }

    else {
        $(this).toggleClass('active-header').toggleClass('inactive-header');
        $(this).next().slideToggle().toggleClass('open-content');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top