user1999510
user1999510

Reputation:

find the closest element of parent element

HTML structure looks like this: title > ul > li > a. After clicking on the anchor tag, I need to .slideToggle() on the .content element, which is closest to the .title.

My current js for it looks like this and it doesn't work. Here's a fiddle

$(function(){ 
  $('.title > ul > li > a').on('click', function(){
    $(this).closest('.content').slideToggle();
  })  
})

Upvotes: 0

Views: 56

Answers (2)

oldhomemovie
oldhomemovie

Reputation: 15129

How about:

$(this).parents('.title').next('.content').slideToggle();

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

.content is not in the same tree as your selector, therefore .closest wont find it. You need to use a combo of .closest and .next

$(this).closest('.title').next(".content").slideToggle();

Fiddle: http://jsfiddle.net/ad9bz/5/

Also, be sure to include jQuery in your fiddle next time!

Upvotes: 3

Related Questions