NicolaeBozbici
NicolaeBozbici

Reputation: 293

slideToggle only clicked element

I have a menu and when I click a link that has a submenu, I want to toggle it. And I did that, however I have more submenus with the same class and when I click one all of them toggles.

I managed to toggle only the clicked element but in this case I need to toggle the children.

Here is the js code I have:

$('li.has-submenu a.link').click(function() { 
$('.submenu').slideToggle(500);
});

And here is a quick fiddle of the situation: http://jsfiddle.net/TV5Kk/

Thanks!

Upvotes: 0

Views: 629

Answers (1)

Venkata Krishna
Venkata Krishna

Reputation: 15112

jsFiddle DEMO

$(this).next('.submenu').slideToggle(500);

Since you have multiple elements with class submenu use $(this) to get the relative element. In your mark up, the next element to the link happens to be one you wanted to toggle.

UPDATE: Since OP wants to automatically slide up all others.

DEMO here

$('li.has-submenu a.link').click(function() {
    $('.submenu').slideUp(500);
    $(this).next('.submenu').slideToggle(500);
});

Upvotes: 3

Related Questions