joshdfw
joshdfw

Reputation: 41

jQuery toggle open/close modify existing code

I'm working with this piece of code, which works, it toggles divs open. However, how can I modify it so that only one div can have the "open" class at a time? Right now you can toggle all the divs open.. But I'd like it so that only one div can be toggled open at any given time. You click another div, the other toggles closed.

Any help/direction is much appreciated!

Thank you!

$('body').on('click','.toggle h3 a', function(){

    if(!$(this).parents('.toggles').hasClass('accordion')) { 
        $(this).parents('.toggle').find('> div').slideToggle(300);
        $(this).parents('.toggle').toggleClass('open');


        //switch icon
        if( $(this).parents('.toggle').hasClass('open') ){
            $(this).find('i').attr('class','icon-minus-sign');
        } else {
            $(this).find('i').attr('class','icon-plus-sign');
        }

        if($(this).parents('.toggle').find('> div .iframe-embed').length > 0 && $(this).parents('.toggle').find('> div .iframe-embed iframe').height() == '0') responsiveVideoIframes();

        return false;
    }
});

Upvotes: 0

Views: 77

Answers (1)

Capevace
Capevace

Reputation: 3

Check out http://jqueryui.com/accordion/.

Since you're already using jQuery check out the premade one.

Upvotes: 0

Related Questions