Languages
[Edit]
EN

JavaScript - clone indicated element

0 points
Created by:
Jacob
532

In this article, we would like to show you how to clone an indicated HTML element using JavaScript.

Quick solution:

Where the boolean value specifies whether to clone the whole subtree (all child nodes) of the element. By default, it's set to false.

Warning:

Cloning an item may lead to duplicate element IDs in a document and these should remain unique!

 

1. Clone element with whole subtree (childNodes)

Edit

In this example, we use cloneNode() method with true value passed as an argument to copy an element with the whole subtree, which in this case is only a text node.

2. Clone element without childNodes

Edit

In this example, we use cloneNode() method with false value passed as an argument to copy an element without its childNodes (the text node).

See also

Edit
  1. JavaScript - get element handle by id

  2. JavaScript - appendChild() method example

References

Edit
  1. Node.cloneNode() - Web APIs | MDN

Alternative titles

  1. JavaScript - clone specified HTML element
  2. JavaScript - copy indicated HTML element
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join