user3609529
user3609529

Reputation: 3

Use jQuery to open clicked item and close any others that are open

Demo: http://jsfiddle.net/KevinOrin/LM4Rf/

I would like any current open items to close when I click on a new item. Clicking on a item thats already open should close that item.

Example I click Leadership it opens, clicking it again should close it.

But if I click Leadership and it opens and then click Investments, Leadership should close and Investments should open.

I keep playing with the jQuery but everything I try breaks something else, how do I refactor this?

// OT accordion function
    var submenu = $('ul.OT-subCats, ul.OT-subsubCats, .OTToggleHide').hide();

    $('#OT-CatAccordion .accSelector').click(function () {
        if ($('ul.OT-subCats, ul.OT-subsubCats').is('visible')) {
            $('ul.OT-subCats, ul.OT-subsubCats').hide('300');
        }
        $(this).find('ul.OT-subCats, ul.OT-subsubCats').slideToggle('300');
    });

Upvotes: 0

Views: 92

Answers (1)

maj
maj

Reputation: 582

Try this

DEMO

$('#OT-CatAccordion .accSelector').click(function () {
    $('ul.OT-subCats, ul.OT-subsubCats').hide(300);
    if (!$(this).find('ul.OT-subCats, ul.OT-subsubCats').is(":visible")) $(this).find('ul.OT-subCats, ul.OT-subsubCats').slideToggle('300');
});

Upvotes: 2

Related Questions