I want to bind an event on some iframes that are dynamically created on a page.
There is a way to bind events on regular elements that don't exist yet on page, using jQuery's .on() method, and passing a selector as second parameter. Like this :
$(document).on('click', 'div', () => {
// some code
});
But iframe behaves differently I guess. This won't work:
$(document).on('click', 'iframe', () => {
// some code
});
Also I thought of these:
$(window).on('click', 'document', () => {});
// or
$(document).on('click', 'body', () => {});
None of them worked.
Also I thought of another approach:
$(document).on('load', 'iframe', e => {
$(e.target).contents().find('body').on('click', () => {
// some code
});
});
Didn't even bind the load event for iframes.
I MIGHT be able to handle it with completely different approach. I can bind the click event else where in code, each time an iframe is created, and they are infact "existed" in the page, then I might be able to bind any event I want to them. But I want to do that as the last solution. If there is a way to bind these events on page load for any iframes that will be created dynamically in the page, please suggest a solution.
UPDATE: I am on the same-domain, so the cross-origin restrictions don't apply.