You are looking for the appendTo
option.
For example, if you want to keep the dialog inside body
$("#wrapper").dialog({
appendTo: "body"
});
In your case I tried this one
$("#wrapper").dialog({
appendTo: "#wrapper"
});
but I've got error, so I suggest to make it like this
$("body").append("<div id='dialog-wrapper'></div>");
$("#dialog-wrapper").html($("#wrapper").html());
$("#wrapper").html("");
$("#dialog-wrapper").dialog({
appendTo: "#wrapper"
});