css - Show the first previous collapsible div using jQuery -


what best (generic) way following using jquery. when clicking anchor within .story-collapse want toggle div .collapse in article resides in. article repeated several times on page , of course current 1 should toggle.

<article>     <div class="story-body clearfix">         <div class="story-paragraph">             <p>this shown</p>             <div class="collapse">                 <p>this hidden</p>             </div>         </div>     </div>     <div class="story-collapse"><a class="icon icon-icon-arrow-up" href="#"></a></div> </article> 

you can use closest:

$('.story-collapse').on('click',function(){    $(this).closest('article').find('.collapse').toggle(); }); 

or, can use prev , find correct element:

$(this).prev().find('.collapse').toggle(); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -