It sounds like you are thinking of scripts as something that
manipulate the HTML text string of a page as opposed to where they
really work which is on the DOM structure that the page is parsed
into.
Lets (for simplicity sake) assume all your nodes have "id"s such that.
hN = document.getElementById('head');
i1N = document.getElementById('img1');
i2N = document.getElementById('img2');
If you want to wrap a div around the two <img> nodes then would do
something like:
myDiv = document.createElement('div');
myDiv.appendChild(i1N);
myDiv.appendCHild(i2N);
hN.parentNode.insertBefore(myDiv, hN.nextSibling);
You don't have to remove the <img> nodes from the body because because
by the logic of DOM manipulation they can only appear in one place.
To instead put i2N and everything that follows into myDiv you just
need to loop through those elements and .appendChild to myDiv, though
you need to be careful how you loop through them because you are
moving them and things like .nextSibling will change after a node is
moved.
http://developer.mozilla.org/en/docs/DOM:element