Grow your CSS skills. Land your dream job.

Remove dynamically appended elements

  • # January 31, 2013 at 5:03 am

    Hey!
    I have a problem with my JS code:

    I want to achieve that every element with the class .activity is removed. What I do is:


    $(".activity").each(function() {
    $(this).remove();
    })

    Basically this works fine the first time. But as the one .activity is removed, I append a new one. And the .remove() function does not work there.

    I saw that there is a method .live() (.on()), but this takes an event like a click. I have no event; I just want to select every appended element and remove it then.

    # January 31, 2013 at 5:09 am

    When do you want the elements to be removed and why are you adding more?

    This is to closer understand your problem and find a good solution for you.

    # January 31, 2013 at 5:16 am

    I do an ajax request loading new data. With this data there come new activities. So i want to remove the old ones and add the new ones.

    I hope this is clearer now :)

    # January 31, 2013 at 5:24 am

    So if I understand it correctly, you’re already tying the small bit of code you’re showing here to the callback function of the ajax, and when you request the info the first time it works, but not the second?

    I think I might need to see the code a bit more, if you could make a [Codepen](http://codepen.io/pen/)?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".