Skip to content

Instantly share code, notes, and snippets.

@nikitaeverywhere
Created May 13, 2017 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikitaeverywhere/53e30f4b7bd067b93a38690227860c08 to your computer and use it in GitHub Desktop.
Save nikitaeverywhere/53e30f4b7bd067b93a38690227860c08 to your computer and use it in GitHub Desktop.
Deep node clone node with styles cloning
function deepCloneWithStyles (node) {
const style = window.getComputedStyle(document.body, null);
const clone = node.cloneNode(false);
if (clone.style && style.cssText)
clone.style.cssText = style.cssText;
for (let child of node.childNodes)
clone.appendChild(deepClone(child));
return clone;
}
@PlopTheReal
Copy link

Heya!
I was looking at this and I wonder how deepClone() looks like ;)
Many thanks!

@nikitaeverywhere
Copy link
Author

@PlopTheReal thank you!

@kiran-venugopal-bs
Copy link

kiran-venugopal-bs commented Apr 17, 2023

what is deepClone here in line 7?

@nikitaeverywhere
Copy link
Author

nikitaeverywhere commented Apr 17, 2023

what is deepClone here in line 7?

As it was 6 years ago and to give you a more qualified answer, can you remind me how did you land on this page? How did you find out this snippet?

Thanks @kiran-venugopal-bs .

@noodlebenji2960
Copy link

i believe its supposed to be a recursive function. "deepClone" should be "deepCloneWithStyles".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment