tonyf
tonyf

Reputation: 35557

Trigger a button click from the parent that exists within an Iframe

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.

Upvotes: 2

Views: 5013

Answers (2)

Mihai T
Mihai T

Reputation: 17687

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");
});

Upvotes: 3

Tamiros
Tamiros

Reputation: 1

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()

Upvotes: 0

Related Questions