Getting Parent of Parent Using JQuery

Go To StackoverFlow.com

2

I have an disappearing delete animation code and I want to get the entire div "parent_parent" to disappear.

Here is the HTML

<div class="parent_parent">
    <div class="parent">
        <a href="?delete=1" class="delete_link"></a>
    </div>
</div>

And here is part of the jquery code that makes the parent_parent div disappear:

$(document).ready(function() {
    $('a.delete_element').click(function(e) {
        e.preventDefault();
        var parent = $(this).parent();
        $.ajax({
            type: 'get',
            url: 'delete.php',
            data: 'ajax=1&delete=' + parent.parent().attr('id').replace('sort_', ''),
            beforeSend: function() {
                parent.parent().animate({
                    'backgroundColor': '#fff'
                }, 300);
            },
            success: function() {
                parent.parent().slideUp(300,function() {
                parent.parent().remove();
                });
            }
        });
    });
});​

But so far no animation happens, but if I just call one parent then the inside div does disappear. I don't get any error messages either.

2012-04-05 02:10
by drummer392
Not sure if its your issue or a miscopy but you are missing a . between the last parent() and slideUp - Loktar 2012-04-05 02:11
Yes, it was miscopying! Thanks for catching that - drummer392 2012-04-05 02:13
Also, in your callback function, parent isn't defined. You need $(this), and then a reference to what you want - Chris Laplante 2012-04-05 02:13
@SimpleCoder nice, I bet thats the issue. Should post that as an answer - Loktar 2012-04-05 02:14
I updated the code...still not getting any results - drummer392 2012-04-05 02:15
No, $(this).parent().parent().remove(); should be $(this).remove(), just as I had it in my cod - Chris Laplante 2012-04-05 02:16
sorry, I had edited this and then I saw your cod - drummer392 2012-04-05 02:17
Got it, changed my code to reflect - drummer392 2012-04-05 02:23
Good. However, I'd suggest that you give each div its own ID. That way, you don't need to mess with parent() and classes - Chris Laplante 2012-04-05 02:25
Yes, the divs do have their down IDs. I'll change the code to reflect that too. Good suggestion - drummer392 2012-04-05 02:27
Then you should just select them based on their IDs. Why didn't you post your full code example to begin with? You could have saved yourself a lot of work - Chris Laplante 2012-04-05 02:28
Trust me, it passed through my mind when I was posting. I'm sorry - drummer392 2012-04-05 02:33
Haha it's ok. Once you post the real code I can give you a much better answer - Chris Laplante 2012-04-05 02:34
Also, do you have more than one of these delete_link structures on your page - Chris Laplante 2012-04-05 02:34
No, just one delete_link structured list on this page - drummer392 2012-04-05 02:36
Ok. Can you please post the actual code, complete with ids for the divs - Chris Laplante 2012-04-05 02:38


4

Your code is still too complicated for what you are trying to do. This is better:

// $(function(){ is shorthand for $(document).ready(function(){
$(function() {
    $('a.delete_element').click(function(e) {
        e.preventDefault();
        // Don't give thing ambiguous names like 'parent'
        // You should change your class names too.
        var container = $(this).closest(".parent_parent");
        $.ajax({
            type: 'get',
            url: 'delete.php',
            // You had multiple instances of parent.parent(), but no direct usage of parent alone
            // This is a clue that you can simplify parent.parent() since you are traversing to the same element every time
            data: 'ajax=1&delete=' + container.attr('id').replace('sort_', ''),
            beforeSend: function() {
                containter.animate({
                    'backgroundColor': '#fff'
                }, 300);
            },
            success: function() {
                container.slideUp(300, function() {
                    // Do not repeat the same selector within a callback
                    // That's what `this` is for
                    $(this).remove();
                });
            }
        });
    });
});​

If you use this code example as it is, it will work.

2012-04-05 02:14
by Chris Laplante
I'm not even getting the slide up animation, let alone the remove. It's saddening - drummer392 2012-04-05 02:17
I think you need to give us some context here. success is probably a callback on an Ajax operation. Please post the code that uses success, since that will determine what the first this refers t - Chris Laplante 2012-04-05 02:18
posted in my question - drummer392 2012-04-05 02:20
I figured it ou - drummer392 2012-04-05 02:24
Don't know why I missed this when I posted my response, but I like it. Caching the original parent jQuery object will also work, but since the performance would be trivially different I think it would boil down to style preference more than anything - Greg Pettit 2012-04-05 02:29
Apparently he does cache the parent object in parent, though at this point I'm not sure what it refers to - Chris Laplante 2012-04-05 02:30
Well, that original question has been edited so many times it's hard to tell what was there and what's the result of suggestions by us. ;-) I don't think it was originally there - Greg Pettit 2012-04-05 02:32
You're right, it wasn't there to begin with. It was just added a few minutes ago - Chris Laplante 2012-04-05 02:33
@drummer392: I've updated my code. You still haven't posted your real code with the ids - Chris Laplante 2012-04-05 14:41


1

You are probably not preventing the default anchor tag action. You probably also want to cache references that you're going to use multiple times. Here's working code:

function(e) {
    e.preventDefault();
    var theParent = $(this).closest(".parent_parent");    
    theParent.slideUp(300, function() {
        theParent.remove();
    });
};

Fiddle: http://jsfiddle.net/VXEUM/

Note also that I'm using closest() instead of doubling up on parent(). Just a style preference. Plus if your element gets nested more deeply it will still work using closest().

2012-04-05 02:20
by Greg Pettit
i figured it ou - drummer392 2012-04-05 02:24
Updated my response; might still be helpful to you to show caching a jQuery object that is reused. Glad it's working. What was the solution? You can post an answer to your own question and even accept it so that someone stumbling on this question could learn. :- - Greg Pettit 2012-04-05 02:27
I will, but I don't have 100 rep so I have to wait like 18 hours - drummer392 2012-04-05 02:34
Ads