Attach an event in a child iframe to a handler in the parent window
Solution 1:
Use contents()
to access the Document object inside an iframe. Note that there are in general problems with using a jQuery library in one document to manipulate another document and it should in general be avoided. However, in the case of binding events it does work.
$('[name=temp_iframe]').contents().find('button').click(function() {
alert('click');
});
This requires that the iframe and its contents are loaded, so do it in a $(window).load()
handler if necessary. You can't live
/delegate
across documents, as events don't propagate from a child document into its parent.
Solution 2:
In plain JavaScript it would be:
document.getElementById("frameId").contentDocument.getElementById("buttonId").click();
If you need to search elements by attribute value and tag name, you can:
document.querySelectorAll('[name=temp_iframe]')[0].contentDocument.getElementsByTagName('button')[0].click();