diff --git a/files/en-us/web/api/document/importnode/index.md b/files/en-us/web/api/document/importnode/index.md index c7c9cca89378a97..04a5a990728c49a 100644 --- a/files/en-us/web/api/document/importnode/index.md +++ b/files/en-us/web/api/document/importnode/index.md @@ -20,6 +20,11 @@ currently in the document tree. Unlike {{domxref("document.adoptNode()")}}, the original node is not removed from its original document. The imported node is a clone of the original. +**`importNode()`** will cause the `document` to immediately adopt the node, in contrast +with {{domxref("Node.cloneNode()#return_value", "Node.cloneNode()")}} which will not +cause the `document` to adopt the node until it is inserted into the document. This makes +**`importNode()`** ideal for working with the {{htmlelement("template")}} element. + ## Syntax ```js-nolint diff --git a/files/en-us/web/api/htmltemplateelement/content/index.md b/files/en-us/web/api/htmltemplateelement/content/index.md index 0ad4c38a622126c..3b3f13a3a02ec47 100644 --- a/files/en-us/web/api/htmltemplateelement/content/index.md +++ b/files/en-us/web/api/htmltemplateelement/content/index.md @@ -20,7 +20,7 @@ A {{domxref("DocumentFragment")}}. ```js const templateElement = document.querySelector("#foo"); -const documentFragment = templateElement.content.cloneNode(true); +const documentFragment = document.importNode(templateElement.content, true); ``` ## Specifications diff --git a/files/en-us/web/api/node/clonenode/index.md b/files/en-us/web/api/node/clonenode/index.md index 810e16e14cb5d0a..0ec189ebbdef3ff 100644 --- a/files/en-us/web/api/node/clonenode/index.md +++ b/files/en-us/web/api/node/clonenode/index.md @@ -28,7 +28,7 @@ Additionally, for a {{HTMLElement("canvas")}} element, the painted image is not > Also, `name` attributes may need to be modified, > depending on whether duplicate names are expected. -To clone a node to insert into a _different_ document, use +To clone a node to insert into a _different_ document, or when cloning a {{htmlelement("template")}} element, use {{domxref("Document.importNode()")}} instead. ## Syntax diff --git a/files/en-us/web/api/web_components/using_templates_and_slots/index.md b/files/en-us/web/api/web_components/using_templates_and_slots/index.md index e8dc42cfc4e005b..9b3e75c1e9cc285 100644 --- a/files/en-us/web/api/web_components/using_templates_and_slots/index.md +++ b/files/en-us/web/api/web_components/using_templates_and_slots/index.md @@ -48,13 +48,13 @@ customElements.define( let templateContent = template.content; const shadowRoot = this.attachShadow({ mode: "open" }); - shadowRoot.appendChild(templateContent.cloneNode(true)); + shadowRoot.appendChild(document.importNode(templateContent, true)); } }, ); ``` -The key point to note here is that we append a clone of the template content to the shadow root, created using the {{domxref("Node.cloneNode()")}} method. +The key point to note here is that we append a clone of the template content to the shadow root, created using the {{domxref("Document.importNode()")}} method. And because we are appending its contents to a shadow DOM, we can include some styling information inside the template in a {{htmlelement("style")}} element, which is then encapsulated inside the custom element. This wouldn't work if we just appended it to the standard DOM. @@ -255,7 +255,7 @@ customElements.define( "element-details-template", ).content; const shadowRoot = this.attachShadow({ mode: "open" }); - shadowRoot.appendChild(template.cloneNode(true)); + shadowRoot.appendChild(document.importNode(template, true)); } }, ); diff --git a/files/en-us/web/html/reference/elements/template/index.md b/files/en-us/web/html/reference/elements/template/index.md index bce849f38f737e1..0128894fce66612 100644 --- a/files/en-us/web/html/reference/elements/template/index.md +++ b/files/en-us/web/html/reference/elements/template/index.md @@ -55,7 +55,7 @@ There are two main ways to use the `