Cloning and Removing
While the world debates about cloning, programmers already have it on the assembly line :)
For cloning an element there's the clone() method — it creates a clone of the element, and you can insert it into the DOM using any of the appropriate methods.
What's more, if you had event handlers attached, you can clone those too — just set the first argument to true:
$("h1").click(() => alert("h1!"))
// clone without event handlers
$("h1").clone()
// clone with event handlers
$("h1").clone(true)clones the selected elements for further insertion of copies back into the DOM
clones the selected elements along with current event handlers and data()
also clones event handlers and data of child elements
Besides cloning, you can detach any element from the DOM to modify and insert it back, or you can even remove it entirely:
detaches the element from the DOM while preserving all its data in jQuery; use this when you only need to temporarily remove an element
removes text and child DOM elements
permanently removes the element from the DOM
Last updated