Reputation: 361
I have a commenting system and it currently folds all replies. I want to show two replies and then fold all of them. now I looked into this http://jsfiddle.net/isherwood/Frpmj/3/ but I'm not quite getting it because I use for-loop for the replies. Here's my code.
<div class="replyInfo">
{% if not comment.is_child %}
<div class="wholeReply">
{% if comment.comment_count %}
<a href='#' class='replies'>
{{comment.comment_count}}</a>
{% endif %}
<div class="got_replies">
<ul style="list-style-type: none;">
{% for child in comment.get_children %}
<li>
{{ child.get_comment }}
</li><hr>
{% endfor %}
</ul>
</div>
</div>
</div>
<script>
$('.replies').click(function(e){
e.preventDefault();
$(this).next(".got_replies").fadeToggle();
})
$(".got_replies").fadeToggle();
</script>
can someone please help me out?
Upvotes: 2
Views: 87
Reputation: 2191
You can use slice()
function to select the first two elements with a class name of reply
. You will have to add the class name reply
to your html of course. Live Preview
HTML with added class name:
<div class="replyInfo">
{% if not comment.is_child %}
<div class="wholeReply">
{% if comment.comment_count %}
<a href='#' class='replies'>
{{comment.comment_count}}</a>
{% endif %}
<div class="got_replies">
<ul style="list-style-type: none;">
{% for child in comment.get_children %}
<li class="reply"><!--added class to your li element-->
{{ child.get_comment }}
</li><hr>
{% endfor %}
</ul>
</div>
</div>
</div>
JavaScript:
$('.replies').click(function(e){
e.preventDefault();
$(this).next(".got_replies").find(".reply").slice(0,2).fadeToggle();
});
$(".reply").fadeToggle();
Upvotes: 1
Reputation: 468
Currently, you are hiding the whole div. I didn't test it, but try this:
<script>
$('.replies').click(function(e){
e.preventDefault();
$(this).next(".got_replies").children("ul").children("li:nth-child(n+2)").fadeToggle();
})
</script>
Upvotes: 0