I have some posts, each post has its own comments.
I use the slice function to only display 5 comments:
$(".comment_section_div").children("div").slice(0, 5).show();
My html content that is in a for loop:
<div class='comment_section_div' answer_index = "{{forloop.counter}}" pk = "{{ answer.pk }}" style="margin: 10px 0px; padding-right:10px;">
<div style="font-size: 12px; color: #737373; margin-top: 15px; direction: rtl;">
<div id = "comment_txt" style="display: inline;">
{{ comment.comment }}
</div>
<div style="display: inline;">
by -
<a href="#" onclick="return false;">
{{ comment.user|find_username }}
</a>
in -
{{ comment.timestamp|pretty_date }}
</div>
</div>
</div>
Each post should display its 5 comments, but only the first post shows them. Actually other posts can also display theirs if I set the slice parameter to a high value, like this:
$(".comment_section_div").children("div").slice(0, 50).show();
but this way the first post shows all of its comments. As it seems this code is not runt relatively, but absolutely.
What is wrong? What is the solution ?
the css part:
.comment_section_div > div{
display:none;
padding: 10px;
border-width: 0 1px 1px 0;
border-style: solid;
border-color: #fff;
box-shadow: 0 1px 1px #ccc;
margin-bottom: 5px;
background-color: #f1f1f1;
}
divwithin each$(".comment_section_div")- the way you are doing it currently is en masse$(".comment_section_div").children("div")returns all children div as a single 'array' which you are then slicing once. You'll need to do something like:$(".comment_section_div").each(function() { $(this).children("div").slice(0,5)...}