Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplest way to show/hide div that shares classes with other divs using jquery

Tags:

html

jquery

css

say my code goes something like:

<div class="click">click</div>
<div class="something">sth</div>
<div class="container"><div class="content">content 01</div></div>
<div class="click">click</div>
<div class="something">sth</div>
<div class="container"><div class="content">content 02</div></div>

with the css being:

.content { display:none; }

but when i click one of the .click, i'd only like the .content below it to show. what's the easiest, most simplest way to do so using jquery? thanks in advance.

like image 714
Ana Avatar asked Oct 18 '22 23:10

Ana


1 Answers

Just bind click event and use any relevant transversal method, e.g:

$('.click').on('click', function(){        
    $(this).nextAll('.container').first().children('.content').show();
});

Now the easiest way would be to wrap all relevant specific elements inside a common container/wrapper element, and then use:

$(this).closest('.wrapper').find('.content').show();
like image 105
A. Wolff Avatar answered Nov 03 '22 23:11

A. Wolff