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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -