2

I have the following path to a button within an iframe, i.e.:

div.iframe-container > iframe#builder-frame > html > body > div#header > ul.header-toolbar.hide > li > span.deploy-button-group.button-group > a#btn-deploy.deploy-button

Using the above path, I'm attempting to use jQuery to get to the a#btn-deploy.deploy-button in order to trigger a click event on this button within my iframe.

I'm unsure how to achieve this using jQuery, in order to get to this button from the parent window, within my javascript code.

I have tried the following but to no avail, i,e,:

$('#builder-frame').contents().find('#btn-deploy').trigger("click");

I understand that this maybe a duplicate but it's actually the path to get to my iframe button that I require assistance with.

halfer
  • 19,824
  • 17
  • 99
  • 186
tonyf
  • 34,479
  • 49
  • 157
  • 246
  • sence your page loads first and then the iframe loads, you need to set the jquery script inside of iframe.load event. check out this: https://stackoverflow.com/questions/13439303/detect-click-event-inside-iframe – Joachim Haglund Jun 14 '18 at 13:54
  • Might help : https://stackoverflow.com/questions/3672726/attach-an-event-in-a-child-iframe-to-a-handler-in-the-parent-window – Hyyan Abo Fakher Jun 14 '18 at 13:55

2 Answers2

3

To be sure your iframe is loaded you should use an on load event.

Then, to select the button you don't have to write the whole path. Especially when it has an id which should be unique.

Your code should look something like :

$("#builder-frame").on("load", function(){
  $(this).contents().find('#btn-deploy').trigger("click");
});
Mihai T
  • 17,254
  • 2
  • 23
  • 32
0

I suggest using pure javascript.

If the id of your iframe is #builder-frame you can use:

document.querySelector('#builder-frame').contentWindow.document.querySelector('#btn-deploy').click()
Tamiros
  • 1
  • 2
  • I do not think this will work but maybe `document.querySelector('#builder-frame').contentWindow.querySelector('#btn-deploy').click()` – Hyyan Abo Fakher Jun 14 '18 at 14:11