Shuvo Habib
Shuvo Habib

Reputation: 2125

jQuery: How to remove appended item on click toggle

I'm appending a class on click of a 'span'. I'm able to remove that appended div on click on other icon and append there. How can I add toggle on every icon clicked twice .This is my jquery below:

  var toggle = true;
$('.extra-items span').on('click',function() {
    $(".mid-side .same-category").remove();
    if(toggle)
    {
        $(this).closest(".row").append($(".media-categories-all").html());
        toggle = false;
    }
    else
    {
        $(this).closest(".mid-side .same-category").remove();
        toggle = true;
    }
})

There are a lot of 'span' also. When it's appended on first click on a icon.. it removes that also in the first click of another icon and nothing is appended in that first click.

Thank you brso05

Upvotes: 0

Views: 404

Answers (1)

brso05
brso05

Reputation: 13232

You can create a global variable and use it to toggle:

var toggle = true;
$('.extra-items span').on('click',function() {
    //removed remove() here
    if(toggle)
    {
        $(this).closest(".row").append($(".media-categories-all").html());
        toggle = false;
    }
    else
    {
        $(this).closest(".mid-side .same-category").remove();
        toggle = true;
    }
});

Upvotes: 1

Related Questions