views:

149

answers:

3

if an element is removed using

$('.notification').remove();

How do we create it back.

+3  A: 

You can't get that particular instance back. Using $.remove() removes it from the DOM. You can create a clone of it, move it around the DOM, hide it, etc., though. Depending on what your project requires, you likely have many other options.

If you're not too interested in that particular instance, you can create a new one. Suppose that was a div with a statement:

$("<div />").addClass("notification").text("I exist!").appendTo("body");

If you want to keep a copy of that particular element around, you can $.clone() it and remove the original:

var clone = $(".notification").clone(); // making zeh' clones!
$(".notification").remove();            // original is gone
$("body").append(clone);                // appears to have returned
Jonathan Sampson
Hi Jonathan,What u mean to say is that i can use `$.remove()` along with `$("<div />").addClass("notification").text("I exist!").appendTo("body");` this statemnet right?
Hulk
@Hulk: I was saying you can remove the elements, and create new ones later. Yes.
Jonathan Sampson
k.Thanks............
Hulk
+4  A: 

Check out the jQuery 1.4 method .detach(). It lets you "remove" elements from the DOM and saves them so they can be reinserted later.

munch
A: 

detach() does what remove() does, with the difference that remove will also take out any event handlers on the element. So long as a reference is made at some point, an element can be removed or detached equally, and be returned to the DOM

noti = $('.notification').remove();
$('body').append( noti ); // later
bibby