Menu Close

How to Clone HTML Element Objects in JavaScript?

Clone HTML Element Objects
To clone HTML element objects in JavaScript, we can call the cloneNode method with true to clone the element. For instance, if we have the following element:
  hello world  

Then we can clone the div by writing:

const clone = document.querySelector('div').cloneNode(true);  
We call document.querySelector to get the div. Then we call cloneNode with true to clone the div and assign it to clone . So clone would have the cloned div.
Posted in HTML, JavaScript

You can also read...