user874185
user874185

Reputation: 853

targeting a button inside iframe

Im trying to target a button on a form inside a iframe but its not working, so my question is how do i target a button thats inside a iframe, any help is appreciated thank you.

this is what I have and ive tested it on my site to make sure jquery was working and it does but im just not able to get the button within the iframe.

  $(document).ready(function(){
        /*iframe name is #innerdiv*/
      $("#innerdiv #btnGoBorrow").click(function(){
        $("#outerdiv").slideToggle();
      });
    });

Upvotes: 0

Views: 1511

Answers (2)

TheEllis
TheEllis

Reputation: 1736

Try $("#innerdiv").contents().find("#btnGoBorrow").click(). Watch out for same origin policy though (i.e. domains, protocols, and ports must match between the iframe and parent page).

Upvotes: 0

Pierre
Pierre

Reputation: 19081

You can't access elements within an iframe. However, elements contained in the iframe can access your page using window.top.

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved with window.postMessage.

Source.

Upvotes: 2

Related Questions