I need to run JavaScript code in iframe. But script with id \"us\" loaded after creating iframe. How to run this javascript in iframe?
The jQuery instance inside of the iFrame doesn't know it's supposed to be traversing the parent DOM, so therefore it won't know where to look for an element with the id "preview"
As per my comments above, you should attach to the iframe's document.ready, like this:
// Since this is page you wait until it's loaded
$(document).ready(function() {
$(".test").click(function() {
alert(true);
});
});
EDIT: Just realizing you are probably having an entirely different issue here - Html code as IFRAME source rather than a URL - if you are trying to embed the iframe code inline, you are going to have problems. Take the following code for example:
Some stuff here
if you render that page in firefox, and then inspect the source in firebug, you'll see:
Some stuff here
This is happening because the browser isn't expecting to see the code inline between the iframe tags.