Update: It seems I didn't need in iframe for my purposes. Also seems jQuery/JavaScript: accessing contents of an iframe answers my question.
I've tried several variations on this and it's refusing to do anything. Sometimes I get an empty iframe, sometimes I get nothing at all.
I have an element with id 'contents' that is a direct child of the body. I wish to create a new iframe within the body, and move 'contents' and all its children into the iframe while preserving the dom hierarchy in context.
My current attempt is:
frame = $("<iframe frameborder=\"1\" height=\"100%\">");
frame.appendTo("body");
$("#contents").detach().prependTo($("body", frame));
But this just erases 'contents' into oblivion. Doing it without 'detach' has the 'contents' just stay put. Removing the 'body' selector from the prependTo erases 'contents' again, without it appearing in the iframe.
EDIT: Seems something is more fundamentally wrong, a simpler code such as the following fails as well:
frame = $("<iframe frameborder=\"1\" height=\"100%\">");
frame.appendTo("body");
$("<b>text</b>").appendTo(frame);