Range: cloneContents() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

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

js
cloneContents()

Parameters

None.

Return value

Examples

js
range = document.createRange();
range.selectNode(document.getElementsByTagName("div").item(0));
documentFragment = range.cloneContents();
document.body.appendChild(documentFragment);

Specifications

Specification
DOM
# dom-range-clonecontents

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
cloneContents

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also