<p>I have html code that looks roughly like this:</p>

<pre><code>&lt;div id="id1"&gt;
  &lt;div id="id2"&gt;
    &lt;p&gt;some html&lt;/p&gt;
    &lt;span&gt;maybe some more&lt;/span&gt;
  &lt;/div&gt;
  &lt;div id="id3"&gt;
    &lt;p&gt;different text here&lt;/p&gt;
    &lt;input type="text"&gt;
    &lt;span&gt;maybe even a form item&lt;/span&gt;
  &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Obviously there's more to it than that, but that's the basic idea. What I need to do is switch the location of #id2 and #id3, so the result is:</p>

<pre><code>&lt;div id="id1"&gt;
  &lt;div id="id3"&gt;...&lt;/div&gt;
  &lt;div id="id2"&gt;...&lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Does anyone know of a function (I'm sure I'm not the first person to require this functionality) that can read and write the two nodes (and all their children) so as to swap their location in the DOM?</p>

In this case,
```
document.getElementById('id1').appendChild(document.getElementById('id2'));
```
should do the trick.
More generally you can use
```
insertBefore()
```
.