Try:
Code:
$(document).ready(function() {
$('div.postwrap div.comments').hide();
$('div.postwrap h3').click(function() {
$(this).parents('div.postwrap').children('div.comments').slideToggle('fast');
});
});
This part of your code:
Code:
$('div.postwrap:eq(0) > div.comments').hide();
is looking specifically for a 'div.comments' that is a direct descendant of a 'div.postwrap' that is alos the first one on thepage. the 'eq(0)' does that. This is not what you want for the code you are using.
This:
Code:
$('div.postwrap div.comments').hide();
will target every 'div.comments' inside a 'div.postwrap'. Better in this case.
In a similar case, this:
Code:
$('div.postwrap:eq(0) > h3').click(function() {
$(this).next().slideToggle('fast');
});
Is looking for any 'h3' that is a direct descendant of the first 'div.postwrap' on the page.
Now this might be a little hard to wrap your head around at first but the 'fix' is all based on the same principle. It's all about the order they come in in the HTML so,
Code:
$('div.postwrap h3').click(function(){
$(this).parents('div.postwrap').children('div.comments').slideToggle('fast');
});
It looks for a 'h3' inside a 'div.postwrap' that has just been clicked. Then it looks for that specific 'h3's parent element that happens to be a 'div.postwrap'. Then it searches that element for it's child element of 'div.comments'. It finds it and it applies the toggle.
So the 'eq(0)' is looks ofr an element in that position, starting at 0. ie html like:
Code:
<div class"apple"></div>
<div class"apple"></div>
<div class"apple"></div>
<div class"apple"></div>
and jQuery of:
Code:
$('div.apple:eq(2)')...
will select the 3rd div in the above code.
the operator of ">" is looking for the next direct matching descendant. In your HTML the stucture is this:
Your HTML structure has a lot of nesting in it so jQuery is not going to find it as it doesn't actually exist in that state.
Make since?
http://api.jquery.com/category/traversing/