1

I want to submit a form that's been created dynamically (by a previous form submit) using AJAX, but I'm not able to get it to work successfully. The generated form has the format shown, and is being created successfully

<div id="1" class="container-element">
    <h3>HEADER</h3>
    <form id="formCELL" name="form_1" action="refresh.php" method="post">
        <div class="panel-body">
            <div class="col-xs-10 com">CONTENT GOES HERE</div>                                                                                                                               
            <div class="col-xs-2 btn"><button class="btn btn-primary" id="refresh" type="submit">Refresh</button>                                                                </div>                                                            
        </div>                                                        
    </form>                                                      
</div>

The functions used to generate the form, and then refresh the content, is here. Right now, the $('#formCELL').submit(function(event) function isn't being entered when the refresh button is clicked.

$(document).ready(function() {

    $('#formMAIN').submit(function(event) {

        //formCELL is generated here through AJAX call.....

    });

    $('#formCELL').submit(function(event) {

        // process the form
        alert("about to submit");
        //AJAX call to go here

        event.preventDefault();
    });

});

I'm guessing that the problem has to do with me having my submit function within $(document).ready, that the form I want to refresh hasn't been created at this point yet, but I'm not sure how to continue from here.

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400

4 Answers4

1

Try:

$(document).on('submit', '#formCELL', function(event) { ... });

I create a jsFiddle for you: JsFiddle

I modify the jsFiddle according your needs. Now first form generate second form that is handled by seconf submit function

Leonard Lepadatu
  • 606
  • 8
  • 14
  • Thank you. I have a few other unrelated issues to fix, but $(document).on is allowing me to edit the content of dynamically created divs. Thank you! – PaulMasterson Sep 13 '16 at 09:33
0

You are right. Move the second submit handler into the first one, after the creation of the form, like this:

$(document).ready(function() {

  $('#formMAIN').submit(function(event) {

    $.ajax(....).done(function(result) {
      //formCELL is generated here ...
      $('#formCELL').submit(function(event) {

        // process the form
        alert("about to submit");
         //AJAX call to go here

         event.preventDefault();
      });
    } 

  });

});
Nicolai Ehemann
  • 574
  • 4
  • 10
0

$(document).ready(function() {

$('#formCELL').submit();

$('#formCELL').on('submit', function(event) {

    // process the form
    alert("about to submit");
    //AJAX call to go here

    event.preventDefault();
});

});

Beginner
  • 4,118
  • 3
  • 17
  • 26
0

Try this

$(document).delegate('#formCELL','submit',function(event) {

        // process the form
        alert("about to submit");
        //AJAX call to go here

        event.preventDefault();
    });
MKB
  • 777
  • 1
  • 9
  • 27