Trigger a button click inside an iframe [duplicate]

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

Leave a Comment