Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  158] [ 1]  / answers: 1 / hits: 117833  / 11 Years ago, wed, april 17, 2013, 12:00:00
<div class=wrap>
<div class=Level2>Click me</div>
<div class=Level3>Information</div>
</div>

<div class=wrap>
<div class=Level2>Click me</div>
<div class=Level3>Information</div>
</div>

<div class=wrap>
<div class=Level2>Click me</div>
<div class=Level3>Information</div>
</div>

<div class=wrap>
<div class=Level2>Click me</div>
<div class=Level3>Information</div>
</div>


jQuery



$('.Level2').click(function(){
$('.Level2').closest('.Level3').fadeToggle();
});


I wanted to select the closest level3 to fadeIn and fadeOut, but doesn't work. Is my syntax wrong? online Sample :http://jsfiddle.net/meEUZ/


More From » jquery

 Answers
53

Try .next() instead of .closest() that traverses through the ancestors of the DOM element.


Working Demo


Also you should use $(this) rather than $('.Level2') else it'll select ALL the .Level2 rather than the clicked one.


You can also go for something like this - $(this).closest('.wrap').find('.Level3').fadeToggle();.


[#78852] Tuesday, April 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileenreynap

Total Points: 140
Total Questions: 106
Total Answers: 99

Location: Andorra
Member since Sun, Oct 18, 2020
4 Years ago
;