window.getSelection().selectAllChildren(test); window.getSelection().getRangeAt(0).extractContents(); does not extract all the contents in test because we canonicalize the selection. For example, if div was: <div id="test"><a href="http://webkit.org/">hello</a></div> The above script results in <div id="test"><a href="http://webkit.org/"></a></div> because the endContainer of getRangeAt(0) is "hello" (TextNode).