I'm writing a plugin for TinyMCE and have a problem with detecting click events inside an iframe.
From my search I've come up with this:
Loading iframe:
<iframe src='resource/file.php?mode=tinymce' id='filecontainer'></iframe>
HTML inside iframe:
<input type=button id=choose_pics value='Choose'>
jQuery:
//Detect click
$("#filecontainer").contents().find("#choose_pic").click(function(){
//do something
});
Other posts I've seen usually have a problem with different domains (this hasn't). But, still, the event isn't detected.
Can something like this be done?
I solved it by doing like this:
I know this is old but the ID's don't match in your code one is choose_pic and one is choose_pics:
Just posting in case it helps someone. For me, the following code worked perfect:
Where 'FileFrame' is the iframe id and 'take_payment' is the button inside iframe. Since my form inside the iframe is posted to a different domain, when used load, I got an error message saying:
I'm not sure, but you may be able to just use
Either that or you could just add a
<script>
tag into the iframe (if you have access to the code inside), and then usewindow.parent.DoSomething()
in the frame, with the codein the parent. If none of those work, try
window.postMessage
. Here is some info on that.In my case there were two jQuery's, for the inner and outer HTML. I had four steps before I could attach inner events:
The trick is to use the inner jQuery to attach events. Notice how I'm getting the inner jQuery:
I had to bust out of jQuery into the object model for it. The
$('iframe').contents()
approach in the other answers didn't work in my case because that stays with the outer jQuery. (And by the way returnscontentDocument
.)If anyone is interested in a "quick reproducible" version of the accepted answer, see below. Credits to a friend who is not on SO. This answer can also be integrated in the accepted answer with an edit,... (It has to run on a (local) server).
fileWithLink.html