jQuery parent().find() problem
Asked Answered
N

2

11

HTML

<div class="comments">
    <a class="toggle" href="#">Toggle Comment 1</a><br />
    <div class="comment" style="display:none;">
        Comment1
    </div>
    <hr />
    <a class="toggle" href="#">Toggle Comment 2</a><br />
    <div class="comment" style="display:none;">
        Comment2
    </div>
</div>

JavaScript

$(function(){
    $('.toggle').click(function() {
        $(this).parent().find('.comment').slideToggle();
        return false;
    });
});

Can be viewed here: http://jsfiddle.net/saiprex/ESM4m/

How i can toggle comment that's been clicked and not all of them?

Cheers, Pav

Ninetta answered 12/5, 2011 at 2:12 Comment(0)
M
13
$(function(){
    $('.toggle').click(function() {
        $(this).nextAll('.comment:first').slideToggle();
        return false;
    });
});

jsFiddle.

Mclane answered 12/5, 2011 at 2:15 Comment(0)
I
0

its even simpler, I think, when you clean up your html as well a little bit: (avoid br)

http://jsfiddle.net/ESM4m/27/

<div class="comments">
    <a class="toggle" href="Fork#">toggle</a>
    <div class="comment" style="display:none;">
        Comment1
    </div>
    <hr />
    <a class="toggle" href="#">toggle</a>
    <div class="comment" style="display:none;">
        Comment2
    </div>
</div>




$(function(){
    $('.toggle').click(function() {
        $(this).next().slideToggle();
        return false;
    }); 
});
Incubate answered 12/5, 2011 at 2:32 Comment(1)
It does work, thanks. But there will be html tags between each comment, so i cant use .next()Ninetta

© 2022 - 2024 — McMap. All rights reserved.