Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.remove() - Is there a way to get the object back after you remove it?

Tags:

jquery

I basically have the same problem in this questions:

Flash Video still playing in hidden div

I've used the .remove jquery call and this works. However, I have previous/next buttons when a user scrolls through hidden/non-hidden divs. What I need to know is, once I remove the flash object, is there a way to get it back other than refreshing the page?

Basically, can this be handled client side or am I going to need to implement some server side handling.

detach() won't work because the flash video continues to play.

I can't just hide it because the video continues to play as well.

like image 297
Jack Marchetti Avatar asked Apr 30 '10 21:04

Jack Marchetti


People also ask

What does remove function do in jQuery?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

What is the difference between remove () and detach () methods in jQuery?

remove() removes the matched elements from the DOM completely. detach() is like remove() , but keeps the stored data and events associated with the matched elements.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

Which jQuery method removes the selected item?

Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.


3 Answers

$myVariable = $("#removeMe").detach();

The .detach() function is explicitly made to take something out of the DOM to be put back in later. It's a good'n.

API Ref: http://api.jquery.com/detach/

like image 157
Alex Mcp Avatar answered Oct 17 '22 02:10

Alex Mcp


Have you tried:

var clone = $("#someDiv").clone(true);
$("#someDiv").remove();
like image 33
David Murdoch Avatar answered Oct 17 '22 02:10

David Murdoch


You can assign it to a variable:

var undo = $('#someDiv')

Then use the value of "undo" to re-insert the item.

$('#placeholder').html(undo)

Perhaps you're better off hiding it instead of removing it.

like image 37
Diodeus - James MacFarlane Avatar answered Oct 17 '22 02:10

Diodeus - James MacFarlane