Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target single instance of a class

Tags:

jquery

I'm using css to hide content that I want to reveal when a person clicks a link. There are multiple instances on the page.

I've set up an example but currently clicking any of the links reveals all of the content.

http://jsfiddle.net/paulyabsley/rHD43/

Thanks

like image 668
Yabsley Avatar asked Jul 01 '12 22:07

Yabsley


2 Answers

Use .siblings() to find an element with the class details that is a sibling of the link's parent element.

$('.item h3 a').click(function () {
    $(this).parent().siblings(".details").toggleClass("display");
});​

DEMO

like image 154
sachleen Avatar answered Oct 02 '22 10:10

sachleen


You could go up to the root element and search for the details.

$(this).parent().parent().find('.details').toggleClass("display");
like image 34
Terry Avatar answered Oct 02 '22 10:10

Terry