I'm looking for a JavaScript function which does the same thing as jQuery's detach()
(detach an element from DOM without removing it). I came across this, but it's not an officially supported function. Does JavaScript have a function similar to .detach()
that is built in?
I'm looking for a JavaScript function which does the same thing as jQuery's detach()
(detach an element from DOM without removing it). I came across this, but it's not an officially supported function. Does JavaScript have a function similar to .detach()
that is built in?
1 Answer
Reset to default 24Something like this?
function detach(node) {
return node.parentElement.removeChild(node);
}
or, even you can just use node.parentElement.removeChild(node)
Brief explanation. From MDN
The Node.removeChild() method removes a child node from the DOM. Returns removed node.
The removed child node still exists in memory, but is no longer part of the DOM. ... you may reuse the removed node later in your code....
detach
. – kshetline Commented Mar 22, 2018 at 2:12