Reputation: 293
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
Reputation: 15112
$(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.
$('li.has-submenu a.link').click(function() {
$('.submenu').slideUp(500);
$(this).next('.submenu').slideToggle(500);
});
Upvotes: 3