I am trying to load a URL into an iframe by doing the following jquery code. The problem is that when I try to find a element of the iframe that was just loaded, it always returns the content that was loaded in the previous click. And the first click is always null. Can someone help me to get around this? Thanks
$(document).ready(function() {
$('.Popup').click(function() {
$('#myiframe')
.attr('src', $(this).attr('href'))
.attr('frameborder', '0')
return false;
});
alert($('#myiframe').contents().find('.name').html())
});
You need to wait for the the fetched URL to load into the iframe before you can fetch contents from it. You can use the the jquery .load() function to do this. Something like this: