简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM