How do I cancel .detach ()?

I am using jQuery 1.5 and the following code to detach li elements with a specific class when a button is clicked. I want to know when this button is pressed again, how to add an item back to the page?

<script> $("#remove").click(function () { $('li.type').fadeOut(300, function() { $(this).detach(); }); });</script> 
+4
source share
2 answers

Question: where on the page do you want to return the item back? If, for example, all li elements are returned inside <ul id="foo"></ul> , you can 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 = []; }); 
+5
source

here u cannot for the loop.

 var demo; $('li.type').fadeOut(300, function() { demo = $(this).detach(); }); $('#replace').click(function() { $("ul#foo").append(demo); }); 
+1
source

All Articles