This is the iframe code:
<div id="OutDiv" class="outerdiv">
<iframe src="http://beta.sportsdirect.bg/checkout/onepage/" id="InnerIframe" class="FrameCSS" scrolling="no"></iframe>
</div>
Here is the HTMl code of the button which the iframe above is calling:
<button type="button" id="SuperWebF1" title="Continue" class="button">Continue</button>
Here is the jQuery function that i use to trigger the click of the button when page is loaded:
$('#SuperWebF1').trigger( "click" );
But this is working like that only when i place the jQuery code inside the source of button and the iframe is calling the button with the script.
I want to make an event which is clicking the button from outside the iframe. Is it possible ?
Thanks in advance!
Answer: Use the jQuery contents() method If you try to detect or capture a click event inside an iframe simply using jQuery click() method it will not work, because iframe embed a web page within another web page. However, you can still do it on the same domain utilizing the jQuery contents() and load() method.
The Button is a widget for iFrame granting you an opportunity to build various buttons for different purposes on your website. There are tons of actions that can be taken after a user clicks the button.
NOTE: .contents() does not work if iframe's src points to cross domain page. More: Cross domain iframe issue and Get DOM content of cross-domain iframe
//execute code after DOM is ready
$(function() {
//find iframe
let iframe = $('#main_iframe');
//find button inside iframe
let button = iframe.contents().find('#main_button');
//trigger button click
button.trigger("click");
});
<!--Add jQuery library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
If you need to manipulate another page inside iframe, search for official API or GET parameters. Example: Youtube Embed Video, Google Maps Embed Api
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With