Range.cloneContents()
The Range.cloneContents()
returns a DocumentFragment
copying the objects of type Node
included in the Range
.
Event listeners added using addEventListener()
are not copied during cloning. HTML attribute events are duplicated as they are for the Node.cloneNode()
method. HTML id
attributes are also cloned, which can lead to an invalid document through cloning.
Partially selected nodes include the parent tags necessary to make the document fragment valid.
Syntax
documentFragment = range.cloneContents();
Example
range = document.createRange(); range.selectNode(document.getElementsByTagName("div").item(0)); documentFragment = range.cloneContents(); document.body.appendChild(documentFragment);
Specifications
Specification |
---|
DOM Standard (DOM) # dom-range-clonecontents |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
cloneContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range/cloneContents