You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

The Range.extractContents() method moves contents of the Range from the document tree into a DocumentFragment.

Event listeners added using DOM Events are not retained during extraction. HTML attribute events are retained or duplicated as they are for the Node.cloneNode() method. HTML id attributes are also cloned, which can lead to an invalid document if a partially-selected node is extracted and appended to the document.

Partially selected nodes are cloned to include the parent tags necessary to make the document fragment valid.


documentFragment = range.extractContents();


Basic example

var range = document.createRange();
var documentFragment = range.extractContents();

Moving items between containers

This example lets you move items between two containers. Select one or more item, and then click "swap" to move them to the opposite container.


<p id="list1">123456</p>
<button id="swap">Swap selected item(s)</button>
<p id="list2">abcdef</p>


body {
  pointer-events: none;

p {
  border: 1px solid;
  font-size: 2em;
  padding: .3em;

button {
  font-size: 1.2em;
  padding: .5em;
  pointer-events: auto;


const list1 = document.getElementById('list1');
const list2 = document.getElementById('list2');
const button = document.getElementById('swap');

button.addEventListener('click', e => {
  selection = window.getSelection();

  for (let i = 0; i < selection.rangeCount; i++) {
    const range = selection.getRangeAt(i);
    if (range.commonAncestorContainer === list1 ||
        range.commonAncestorContainer.parentNode === list1) {
      const documentFragment = range.extractContents();
    } else if (range.commonAncestorContainer === list2 ||
        range.commonAncestorContainer.parentNode === list2) {
      const documentFragment = range.extractContents();



Specification Status Comment
The definition of 'Range.extractContents()' in that specification.
Living Standard No change.
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.extractContents()' in that specification.
Obsolete Initial specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
extractContentsChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also