954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to add a div in jquery even if you have just used the .remove() function on it

Hi,
Below is my code which I am using to remove a div tag. I also want the script to add the same div tag later once certain conditions are meet. The div has some preloaded content too which I don't want to loose. I tried the add() function but it did not work for me. I even don't think append will work as I want to original div to reappear.

$('#wrapper1').remove();


Any help will be highly appreciated.

Cheers, Vishal

vishalkhialani
Junior Poster
127 posts since Mar 2007
Reputation Points: 47
Solved Threads: 0
 

Vishal,

As it says in the jQuery API, jQuery.remove() returns jQuery , so you can do something like this:

var x = $('#myDiv').remove();
//.......
$('#myElement').append(x);//or .after() or .prepend() depending on how you need to insert x back into the DOM. Of course, like any javascript member, x must be in scope, or formally passed to a function that operates on it.

If x is neither directly in scope, kept alive by closure nor global, then it will be inaccessible and/or lost to garbage collection.

If scope issues make things difficult, you could alternatively move myDiv inside a hidden DOM element (eg another div reserved for this purpose) from which it can be recovered at any point in the code regardless of scope (the DOM is effectivey global). The move in would typically be performed with .append() while the move back out would be performed with .append(), .after() or .prepend().Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Vishal,

As it says in the jQuery API, jQuery.remove() returns jQuery , so you can do something like this:

var x = $('#myDiv').remove();
//.......
$('#myElement').append(x);//or .after() or .prepend() depending on how you need to insert x back into the DOM. Of course, like any javascript member, x must be in scope, or formally passed to a function that operates on it.

If x is neither directly in scope, kept alive by closure nor global, then it will be inaccessible and/or lost to garbage collection.

If scope issues make things difficult, you could alternatively move myDiv inside a hidden DOM element (eg another div reserved for this purpose) from which it can be recovered at any point in the code regardless of scope (the DOM is effectivey global). The move in would typically be performed with .append() while the move back out would be performed with .append(), .after() or .prepend().Airshow

thanks, Airshow . I used the the .append() and it worked :)

vishalkhialani
Junior Poster
127 posts since Mar 2007
Reputation Points: 47
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: