admin管理员组

文章数量:1325235

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 ?

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 ?

Share Improve this question edited Mar 26, 2014 at 11:24 rvandoni 3,4075 gold badges34 silver badges48 bronze badges asked Mar 26, 2014 at 11:18 V-XtremeV-Xtreme 7,3339 gold badges40 silver badges80 bronze badges 1
  • 1 You could simplify your logic using .detach() api.jquery./detach Be aware than using .detach() data will be kept, as e.g, handlers bound to element or children. This is usually preferred/more suitable method – A. Wolff Commented Mar 26, 2014 at 11:33
Add a ment  | 

1 Answer 1

Reset to default 11

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

本文标签: javascriptRemove and append DOM elementStack Overflow