0

I have this form inside test.html file

<form name="myForm1" id="myForm1" action="" method="post" onsubmit="">
    <input type="submit" value="YES" class="buttonclass"> 
</form>

I load it using another file called iframe.html

<iframe id="iframeID" src= "test.html"></iframe>

Now how can I automate the click of the submit button in the iframe using JavaScript ?

2
  • Have you tried or researched anything? Commented Feb 22, 2017 at 19:57
  • I tried but because this one doesn't have name=myButton, I cannot understand how to Commented Feb 22, 2017 at 19:58

2 Answers 2

2
document.querySelector('#iframeID').contentWindow
  .document.querySelector('#myForm1').submit();
Sign up to request clarification or add additional context in comments.

3 Comments

:) if this works, I like it better than my solution. I haven't tried to do this in a long while.
@Pavlo, is there no need to use $( document ).ready kind of approach in case the page/url inside the iframe is going to take time to load ?
I used it like this but it didn't work <html> <head> <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script> $( document ).ready(function() { document.querySelector('#iframeID').contentWindow.document.querySelector('#myForm1').submit(); }); </script> </head> <body> <iframe id="iframeID" src="http://localhost:8080/test.html"></iframe> </body> </html> test.html <form name="myForm1" id="myForm1" action="www.google.com" method="GET" onsubmit=""> <input type="submit" value="YES" class="buttonclass"> </form>
-1

From an old project....

Put this in your iframe code (test.html):

function getElement(id) {
    return document.getElementById(id);
}

then access the element via

var el = document.getElementById("iframeID").contentWindow.getElement(id);

You should be able to act on that element from there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.