Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introduction to jQuery DOM Traversal parent()

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Using fade()out but I still want to completely remove the element.

Hello!

I have a problem. I'm trying to work out how to do a fadeOut remove of the card instead of a simple removal.

    //use of traversal parent method
    $('.close').on('click', function() {
       $(this).parent().fadeOut(400).delay(400).remove();
    });

It's a shame you can't put add an integer to .remove but I thought I was being clever when I added the delay before as it's supposed to add a delay period for the given number of milliseconds and then remove the element, right?

But it only as the same effect as if only the remove element was on the click event. I've tried to look at the docs to see if there's anything on this but I wonder if it's just the normal behaviour of the remove method to do this?

4 Answers

Hi Jonathan,

The fadeOut method takes a second argument which is a function that you want to run when the animation completes.

$(this).parent().fadeOut(400, function(){
    $(this).remove();
});

Delay only works with the animation queue. You would have to add a function to the queue which calls the remove method in order to get it to work like you were trying. But that's kind of a round about way to do what you're trying to do.

If you're interested in the queue it would be something like this.

$(this).parent().fadeOut(400).queue(function(){
    $(this).remove();
});

This adds the function to the queue so that when the fadeOut is complete the next thing in the queue will run. You don't need the delay in this case if you wanted it to be removed immediately after.

I would take the more direct approach though of passing in a complete function as the second argument to fadeOut()

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yup, this worked thanks, I've learned a bit from this,.. including look a bit harder at the documentation :D

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Why not?

The video originally wants us to use remove the card that we add from the screen and the DOM which is what remove does.

Adding fadeOut (in theory) allows us to have a little more control on how this looks visually. We don't have to do this byt I;m just trying to experiment! :-)

Brandyn Lordi
Brandyn Lordi
17,778 Points

Late to the party, but I was also wondering how to achieve this effect. The post above regarding "queue" seemed a bit out of my league, so i fiddled around and achieved it with the following code using functions that we have covered so far to this point. Here's my code for those wondering.

$('.close').on('click', function() {
  $(this).parent().parent().fadeOut(1600, function(){
  $(this).remove()})
});

You can probably tell how i did it just from looking at the code, but I passed an anonymous function through fadeOut() as a parameter that would remove() the parent of the parent of $this, because the direct parent of $this is only the div element, and not the entire section!