I have an iframe which is created after loading page. (main DOM)
var object_preview = $('#preview');
var iframe = $('<iframe id="preview_frame">'),
iframe_body,
iframe_head;
object_preview.html(iframe);
setTimeout(function() {
var doc = iframe[0].contentWindow.document;
iframe_body = $('body', doc);
iframe_head = $('head', doc);
}, 1);
When you click on a button, in the iframe head inserted JS-code (jQuery), which must be executed within the iframe DOM.
$('#run').click(function() {
iframe_body.append('<script type="text/javascript">\
$(document).ready(function() {\
$(".test").click(function() { alert(true); });\
});\
</script>');
});
Now it doesn't work. How to implement it?
Element with class .test is within iframe DOM.
HTML structure:
Thanks in advance.