[jQuery] How dynamically create iframe and add/remove data from it

[jQuery] How dynamically create iframe and add/remove data from it


Hi folks, hoping for some knowledgeable assistance with this problem.
What I'm doing is dynamically creating an iframe in the DOM, then
trying to write some data into it. That's the part I'm stuck at.
I *am* able to create the iframe, no problem. I have a div with the id
#container built into the page in which the iframe is added:
$
(document.createElement('iframe')).attr('id','commentiframe').appendTo('#container');
At this point, I don't know how to write data into the iframe that has
been created. I've been trying to use this but to no success.
var f = $('#commentiframe')[0];
// get iframe's document
var doc = f.contentWindow ? f.contentWindow.document :
f.contentDocument ? f.contentDocument : f.document;
var $framebody = $('body',doc);
var $framebody.html('<h2>test</h2>');