views:

173

answers:

2

I'm trying to write a short Wordpress JQuery for Wordpress comments that would allow users to toggle specific comments on and off. This is my first script, and I'm having a tough time.

In the "comment_options" DIV is a series of buttons that control the individual comments (reply, quote, edit, close, etc.). The close button is what I'm trying to write this script for. I need it to toggle the "gravtar" and "comment_content" DIVs, but leave the rest in place so that it still displays the user ID and controls.

However, I can't seem to figure out how to contain the action.

[EDIT] Here is the updated code:

// Custom comments callback
    function steelfrog_comments($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment; ?>

        <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>" class="comment_comment">

            <div class="comment_info">  

                <div class="gravatar">
                    <?php echo get_avatar($comment,$size='80',$default='<path_to_url>' ); ?>
                </div>

                <div class="comment_poster">
                    <h5><?php comment_author_link(); ?></h5>
                    <span><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></span>
                </div>

                <div class="comment_options">
                    <ul class="options">
                        <li class="reply"><a href="#" title="Reply to this comment"><span>Reply to this comment</span></a></li>
                        <li class="quote"><a href="#" title="Quote and reply this comment"><span>Quote this comment</span></a></li>
                        <li class="link"><a href="#" title="Link to this comment"><span>Link to this comment</span></a></li>
                        <li class="website"><a href="#" title="Website of commentor"><span>Website of commentor</span></a></li>
                        <li class="edit"><a href="#" title="Edit this comment"><span>Edit this comment</span></a></li>
                        <li class="close"><div class="trigger"><a href="#" title="Remove this comment until the page is refreshed"><span>Remove this comment until the page is refreshed</span></a></div></li>
                    </ul>
                </div>
            </div>

            <div class="comment_content">
                <?php comment_text() ?>
            </div>
        <?php } 

And the current JQuery script:

$(document).ready(function(){
    $("div.trigger").click(function() {
        $(this).parent.parent("li").children(".gravatar, .comment_content").slideToggle();
    });
});

[EDIT] Got it working by using two separate selectors, but it sure is ugly.

$(document).ready(function(){

    $(".trigger").click(function(){
        $(this).parent().parent().parent().parent().parent().children(".gravatar, .comment_content").slideToggle('300');
        $(this).parent().parent().parent().parent().children(".gravatar").toggle('300');
        return false
    });

});
+1  A: 

You want to find the divs in relation to the trigger element, like this

$("div.trigger").click(function() {
  $(this).closest("li:not(.close)")
         .find(".gravatar, .comment_content").slideToggle();
});

This climbs to the <li> wrapping the .trigger then looks for those divs inside it.

What you had before, like this: $("div.gravatar"), searches for all <div class="gravatar"> instead of just within a certain element, that's what the code above will change.

Nick Craver
Thanks for the reply! I've edited the example. The trigger is found in the "comment_options" DIV, so I'm assuming this wouldn't work, unless it searches in both child and parent elements and returns the closest in both directions?
steelfrog
@steelfrog - `.closest()` finds the nearest parent matching that selector, so it goes up to the `<li>` then down to the `<div>` elements you want inside. Try it, should work just fine :)
Nick Craver
[EDIT] Oh, guess I can't post code in a reply. Anyhow, I tried it and it doesn't seem to work. I'm not sure why exactly. It sounds pretty logical to me.
steelfrog
@steelfrog - Is there an `<li>` inside the `.comment_options` div? If so can you update the question with the html?
Nick Craver
Updated the original post. Also placed a link to a live page if that is of any help.
steelfrog
@steelfrog - That was indeed the issue, try the updated answer, we're now going up to the `<div class="comment_info">` and back down to get the elements.
Nick Craver
@Nick Craver, Now that I look at it closer, I posted it wrong originally. The "comment_content" div is outside "comment_info". The only related parent would be the LI we tried earlier. Any reason why that wouldn't work?
steelfrog
@steelfrog - Ah I see, yes I updated the answer to find that `li` instead of the one immediately wrapping the div...we need the class of that `<li>` though, so put in your same php variable.
Nick Craver
@Nick Craver, Can I use a generic class that Wordpress automagically places in (in this case, li.comment)? I'm assuming that it should still select it without a hitch, but that didn't seem to work.
steelfrog
@steelfrom - If you don't control that, change this: `$(this).closest("li.<?php comment_class(); ?> ")` part, to this: `$(this).parent().parent("li")` and it should work.
Nick Craver
@Nick Craver, Still no go. Thank you for your patience with this. It's really helping me understand how JQuery manipulates DOM. I still can't figure out why this isn't working, though. Updated the original post with the Wordpress PHP callback and the JQuery script I'm using. Maybe it's something with improper markup on my end?
steelfrog
@Nick Craver, Actually, I got it working by manually scaling up parent elements. See the original post. It's quite ugly, but it does work.
steelfrog
@steelfrog - Just got a chance to look at this again, I updated the answer, tested and working against your site :)
Nick Craver
@Nick Craver, that worked perfectly! Thanks again for your time!
steelfrog
@steelfrog - Welcome! If you're all set please click the checkmark beside the answer the helped you resolve the issue, same for future questions, gives you rep, the answerer rep and heps the next person googling find the answer quicker :)
Nick Craver
A: 

why not just hide the complete div.trigger ? ^^ ok maybe this doesnt work for you... you could do

$("div.trigger").click(function() {
    $("div.trigger > div").slideToggle();
});

this will hide all divs under div.trigger

Nexum
The div's are under trigger...re-read the question :) Also, your answer would hide all divs under any trigger.
Nick Craver
Unfortunately, I need to keep the "gravatar" and "comment_poster" divs displayed.Also, the trigger div is a child of "comment_poster". Sorry, I should have made that clearer when I posted the question.
steelfrog