0

I need to click the button (without an id and name) inside an iframe (with an id and name) with using JavaScript/jQuery.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript">
</script>
</head>
<body>
<iframe name="abb" id="abb" src="http://example.com" scrolling="no"frameborder="0" style="border:none; overflow:hidden; width:45px; left:-19px; height:21px; z-index: 0; position: relative;" allowTransparency="true"></iframe>
</div>

<script>
document.getElementById("abb").contentWindow.document.getElementsByName('REG_BTN')[0].click();
</script>
</body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • possible duplicate of [Intercept click on content in an iframe](http://stackoverflow.com/questions/2085752/intercept-click-on-content-in-an-iframe) – Farid Nouri Neshat Mar 19 '14 at 14:28

3 Answers3

3

You can't access the DOM of documents on other origins.

The closest you could come would be to send a message to the document which would have to include JS that listened for the message and responded accordingly.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

you could always traverse it by using id or name of the tag that you do know

for example

$('body div.container div.panel>div.heading~div button').click();

dsymquen
  • 584
  • 1
  • 5
  • 23
0

This is a way to locate the button:

var iframe = document.getElementById('iframeId'),
    innerDocument = iframe.contentDocument || iframe.contentWindow.document,
    button = innerDocument.getElementsByTageName('input')[0];

This will only work if the iframe origin from your server. This will find the first button, or <input>. If there's more than one <input> you could make a for loop and check for the 'button' type with getAttribute('button');. This will help you doing the click simulation.

Community
  • 1
  • 1
fzzle
  • 1,466
  • 5
  • 23
  • 28