The code below will show / hide a div on click. It works perfectly for the first div listed, but doesn't work for any of the others. Is there a way to have the function apply to all the elements with the same class names? It should, of course, only open / close the div to which it's being applied (i.e., clicking on the Second Div toggle button should only open / close the Second Div, and not the others...)
// this is the markup
<div class="collapsible-item">
<div class="collapsible-item-title">
<div class="item-title-header"> First Div</div>
<img src="/images/expand.png" alt="Expand this section" class="toggle-button">
</div>
</div>
<div style="display: none;" class="togglethis">
Cras cursus sodales odio, quis consectetur felis ultricies in.
</div>
</div>
<div class="separator"></div>
<div class="collapsible-item">
<div class="collapsible-item-title">
<div class="item-title-header"> Second Div</div>
<img src="/images/expand.png" alt="Expand this section" class="toggle-button">
</div>
</div>
<div style="display: none;" class="togglethis">
Cras cursus sodales odio, quis consectetur felis ultricies in.
</div>
</div>
<div class="separator"></div>
<div class="collapsible-item">
<div class="collapsible-item-title">
<div class="item-title-header"> Third Div</div>
<img src="/images/expand.png" alt="Expand this section" class="toggle-button">
</div>
</div>
<div style="display: none;" class="togglethis">
Cras cursus sodales odio, quis consectetur felis ultricies in.
</div>
</div>
And, this is the jQuery:
$(document).ready(function () {
$('.toggle-button').toggle(
function() {
$(this).attr('src', '/images/collapse.png');
$(this).parent().siblings('.togglethis').slideToggle('slow');
},
function() {
$(this).attr('src', '/images/expand.png');
$(this).parent().siblings('.togglethis').slideToggle('slow');
}
);
$('.item-title-header').toggle(
function() {
$('.toggle-button').attr('src', '/images/collapse.png');
$(this).parent().siblings('.togglethis').slideToggle('slow');
},
function() {
$('.toggle-button').attr('src', '/images/expand.png');
$(this).parent().siblings('.togglethis').slideToggle('slow');
}
);
});