This question is based on my previous question which I got a working answer to, but only for one slidetoggle element: link
I use the animate method to have the "hidden" elements loaded in the background. Otherwise I could just use slidetoggle, but this leads to a display:none which I don't want.
So, here's the function I got so far, but it only runs once for each h2.
HTML:
<h2>Show</h2>
<div class="content">
text text text
<br />
text text text
<br />
text text text
</div>
<h2>Show</h2>
<div class="content">
text text text
<br />
text text text
<br />
text text text
</div>
CSS:
.content {
height: 0;
overflow: hidden;
}
.heightAuto {
height: auto;
}
SCRIPT:
$(function(){
$("h2").toggle(function()
{
var $content = $(this).next(".content");
var contentHeight = $content.addClass('heightAuto').height();
$content.removeClass('heightAuto');
$content.removeClass('heightAuto').animate({ height: contentHeight}, 500);
}, function() {
$(this).next(".content").animate({ height: "0"}, 500);
});
});
Could it be a problem to get the height set to auto again? I just can't find the trick.
Here's also fiddle: jsfiddle