Submit form on Dialog Save. Form contained inside an iframe. "Save" button is not triggering the form Submit

I am playing with a simple example where I have a 2 pages

One page is a simple Form page. That uses jQuery submit() to fire up an alert (“yayyy for summitted”) when the form is submitted.

Plunker. Running this code

Page two, places this form in an iframe and loads it into a jQuery Dialog that has a SAVE and CLOSE button

I want to be able to trigger the form Submit using the SAVE BUTTON in the Dialog.
Since the form is in a iframe, I have no idea how to successfully fire up that alert (“yayyy for summitted”).

Been stuck on this for a couple of days. So some help would be nice, thanks…


<!DOCTYPE html>
    <title>iframe in Dialog</title>
    <link rel="Stylesheet" type="text/css" href="" />
    <script  type="text/javascript" src="">    </script>
    <script type="text/javascript" src="">    </script>

    <script type="text/javascript">

        $(document).ready(function () {

            $('a#pop').on('click', function (e) {
                var pagetitle = $(this).attr("title")
                var page = $(this).attr("href")

                var iframe = $('<iframe id="myiframe2" style="background:#EEE9E7; border: 0px; " src="' + page + '" width="100%" ' +

                var dialog1 = $('<div></div>').append(iframe).appendTo('body').dialog({
                        autoOpen: false,
                        modal: false,
                        height: 625,
                        width: 500,
                        title: pagetitle,

                        close: function (e, ui) {
                        buttons: {
                            "Save": function () {
                                alert('Dialog SAVE pressed but for has not yet been subbmitted');
                                var commentForm = '#commentForm';
                                var iFrameDOM = $("iframe#myiframe2").contents();

                                // NOT WORKING
                            "Close": function () {

	<!-- Form Page must reside in same domain -->
    <br /><br /><a id="pop" href="http://localhost/test/formtest" title="Hello World!!!">click me for the Form</a>


Form Page

<!DOCTYPE html>
    <script src="">

                alert("yayyy for summitted");

            //    alert("form is submitted.");

<h2>A very Simple Forrm</h2>
<p>On Submit we use jQuery submit() Method and pop up an alert</p>
<form id="commentForm" name='commentForm' method="get"  action="/">
    Your Name: <input type="text" name="name" value="Santa Clause"><br>
    Your Age: <input type="text" name="age" value="35"><br><br>
    <input type="submit"  name="submit" id="mybutton"  value="Submit">

Hi @rudyten, you can’t access the iframe DOM as if it was on the same page, but via its contentDocument property (provided it has the same origin as the parent page):


Or all jQuery:

var iFrameDOM = $('#myiframe2').prop('contentDocument')

Thanks for the reply, but I am still getting the same behavior, the form is not getting submitted.
I modified the plnkr code.

//var iFrameDOM = $("iframe#myiframe2").contents();
  var iFrameDOM = $("iframe#myiframe2").prop('contentDocument')

Ah, you have given your submit button a name="submit", which causes form.submit refer to that element rather than to the method of the same name. It appears that jQuery will silently swallow this issue, but with vanilla JS you’ll get a just error message:

var iFrameDOM = document.getElementById('myiframe2').contentDocument
var commentForm = iFrameDOM.getElementById('commentForm')

// Uncaught TypeError: commentForm.submit is not a function

Also note that calling submit() will not dispatch a submit event, so you won’t get your “Yayy” alert. To fix this, either use requestSubmit() or trigger a click event on the submit button:

var iFrameDOM = document.getElementById('myiframe2').contentDocument

// V1
var commentForm = iFrameDOM.getElementById('commentForm')

// V2
var submitButton = iFrameDOM.getElementById('mybutton')

For details see here:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.