wyc
wyc

Reputation: 55273

How to select the following nearest element with jQuery?

This is the structure:

enter image description here

I want toggle ul.children by clicking a.exchand-cat-item. I tried this:

$('a.expand-cat-item').on('click', function() {
  $(this).siblings('ul.children').toggle('slow');
});

But then realized that it doesn't work because ul.children is one level up in the DOM tree of a.exchand-cat-item

What's the right way of doing this?

Upvotes: 0

Views: 49

Answers (2)

Dennis
Dennis

Reputation: 32598

Then go up one level and use siblings:

$(this).parent().siblings('ul.children');

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102753

You could use closest to first climb back up to the div element:

$(this).closest('div').siblings('ul.children');

Upvotes: 3

Related Questions