This does not work. .html() returns a string, not a jQuery object. Therefore you cannot call wrap on it.
Another problem is that .html() returns only internal HTML, it does not include the table tag.
You can .clone() node, attach it to some dummy element and return .html() :
var html = ['<html><body>', $('<div/>').append($('#mytable').clone()).html(), '</body></html>'].join('');
source share