Access to inner iframe

by wao20

HTML

<button id="btn">Click Me</button>

JavaScript

// I borrowed the injection script from here: 
// http://sparecycles.wordpress.com/2012/03/08/inject-content-into-a-new-iframe/
var newIframe = document.createElement('iframe');
newIframe.width = '400';newIframe.height = '200';
newIframe.src = 'about:blank'; 
newIframe.id = 'f123';
document.body.appendChild(newIframe);

var myContent = '<!DOCTYPE html>'
    + '<html><head><title>My dynamic document</title></head>'
    + '<body><h1>Hello world</h1><input id="secret" type="text" value="Luke, I am your father" /></body></html>';

newIframe.contentWindow.document.open('text/html', 'replace');
newIframe.contentWindow.document.write(myContent);
newIframe.contentWindow.document.close();

$('#btn').click(function(){
    var txt = $('#f123').contents().find('#secret');    
    alert(txt.val());
});