chemnteach
chemnteach

Reputation: 395

jQuery .submit() not working with javascript-generated html form

I am having a bear of a time figuring out why a jQuery .submit() is not recognizing the submit.

The HTML form (actually multiple forms) are successfully generated into a div that is appended to the page with the following javascript function (this is called out of $(document).ready(function(){}):

function makeMenu(chambers,bins,values) {
    var htmlString = "";
    for(var i = 0; i < chambers.length; i++) {
        if(bins[i]) {
            var link = chambers[i] + " - " + bins[i];
            var p = values[i];
        }

        htmlString += "<form name='entity' id='entity' method='post' action=''>";
        htmlString += "<input type='submit' name='get_entity' id='to_php' value='" + link + "' class='text_button' title='value=" + p + "'>";
        htmlString += "<input type='hidden' name='tool' value='" + chambers[i] + "'/>";
        htmlString += "<input type = 'hidden' name='bin' value='" + bins[i] + "'/>";
        htmlString += "</form>";
    }

    $("#spacer").append("<div class='menu'>" + htmlString + "</div>");
}

Upon submitting a form I am just trying to get an alert to post to show that the submit is recognized but it does not get there:

$(document).ready(function() {
    $('#entity').submit(function() {
        alert('Success!');
    });
 })

I have tried making sure that the submit button did not have submit as the name and id and tried just generating a single form but no luck. Any assistance is much appreciated.

Upvotes: 0

Views: 1007

Answers (2)

Dan
Dan

Reputation: 526

Try on instead of submit. As the form is created by jQuery it isn't bound to the document yet.

$(document).ready(function() {
    $(document).on('submit','#entity',function() {
        alert("success");
    });
 })

Upvotes: 0

Alexandre Wiechers Vaz
Alexandre Wiechers Vaz

Reputation: 1617

Try

$(document).ready(function() {
    $(document).on('submit','#entity',function() {
        alert('Success!');
    });
 })

Upvotes: 3

Related Questions