Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I undo .detach()?

I'm using JQuery 1.5 and the following code to detach li elements w/ a certain class when a button is clicked. What I want to know is, when that button is clicked again, how do I add the element back onto the page?

    <script>
    $("#remove").click(function () {
      $('li.type').fadeOut(300, function() { $(this).detach(); });
    });</script>
like image 932
binaryorganic Avatar asked Apr 23 '11 12:04

binaryorganic


1 Answers

The question is: where on the page do you want to put the element back? If, for example, all the li elements go back inside <ul id="foo"></ul> you might use something like this:

var items = [];
$('li.type').fadeOut(300, function() {
     items.push( $(this).detach() );
});

$('#replace').click(function() {
    for(var i = 0; i < items.length; i++) {
        $("ul#foo").append(items[i]);
    }
    items = [];
});
like image 115
VoteyDisciple Avatar answered Oct 10 '22 05:10

VoteyDisciple