Pregunta

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();
  })  
})
¿Fue útil?

Solución

.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!

Otros consejos

How about:

$(this).parents('.title').next('.content').slideToggle();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top