V-Xtreme
V-Xtreme

Reputation: 7333

Remove and append DOM element

I want to add and remove the element from the DOM . For remove I am using JQuery remove() function . Before removing the element I am coping that element using clone() method :

element = $("#list-view").clone();      
$("#list-view").remove();   

This is working perfectly . But In another case I want to append the same element to the DOM again . So I am using the element which I have cloned earlier :

document.getElementById("container").appendChild(element);

But nothing is happening while appending the element . Am I missing anything ?

Upvotes: 1

Views: 2837

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

element is a jquery object, not a dom element so use

element.appendTo('#container')

Demo: Fiddle

In your console you should be getting the error Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null.

Demo: Fiddle

Upvotes: 11

Related Questions