HTML:
<div class="portlet">
<h3 class="">Blogs</h3>
<div class="portlet-content">
<div class="teaser">
<div class="image-with-caption">
<img src="blogs1_peq.jpg" alt="">
</div>
<p> Some text here </p>
<ul class="link-list">
<li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://theenergycollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">The Energy Collective</a></li>
<li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://sustainablecitiescollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Sustainable Cities Collective</a></li>
</ul>
</div>
</div>
I need to create a sort of accordion. The idea is to display only the headeline and the image inside the div with the class image-with-caption. I made this code in jQuery to wrap everything bellow the div of the image and hide it:
var $createDiv = $('.image-with-caption').nextAll();
for(var i=0, len = $createDiv.length; i < len; i+=2){
$createDiv.slice(i, i+2).wrapAll('<div class="master" />');
}
And I get something like this:
...
<div class="image-with-caption">
<img src="blogs1_peq.jpg" alt="">
</div>
<div class="master">
<p> Some text here </p>
<ul class="link-list">
<li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://theenergycollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">The Energy Collective</a></li>
<li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://sustainablecitiescollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Sustainable Cities Collective</a></li>
</ul>
</div>
...
When I click on the headline it should show me the new div created and hidden by jQuery, but I can’t figure out how can I do this.
jQuery Code:
var $createDiv = $('.image-with-caption').nextAll();
for(var i=0, len = $createDiv.length; i < len; i+=2){
$createDiv.slice(i, i+2).wrapAll('<div class="master" />');
}
$('.master').hide(); //Hide/close all containers
//On Click
$('.portlet h3').click(function(){
if( $(this).nextAll().is(':hidden') ) {
$(this).removeClass('active').next().siblings().next().slideUp();
$(this).toggleClass('active').next().siblings().next().slideDown();
} else {
$(this).removeClass('active').siblings().next().siblings().slideUp();
}
return false;
});
The problem is, I sincerely don’t know how to tell jQuery to display the div with the class master bellow the h3 I just clicked. Unfortunately I can’t change the HTML code, since this code is generated by a CMS, and I have several div.portlet in the same page, so the code will affect all of them, and that’s what I want.
🙁
You could add this
$(this).parent('.portlet').find('.master').show();to the click event for theh3