0

My code builds a dynamic list similar to this:

form = "";
form+= "<li>";
form+= "<form id='formid_" + itemno + "' class='form' method='post'>";
form+= " some options go here ";
form+= "<button type='submit' class='selection'>Select This Option</button>";
form+= "</form>";
form+= "</li>";
form+= "<li>";
form+= "<form id='formid_" + itemno + "' class='form' method='post'>";
form+= " some other options go here ";
form+= "<button type='submit' class='selection'>Select This Option</button>";
form+= "</form>";
form+= "</li>";
$("#my-output-div").append(form);

The problem is that I need to use the correct list items form ID to get the correct selection when the submit button is pressed, but that form ID is dynamic. I know I can use var formID = $(this).parent().attr("id"); to get the ID of the form on click but what is the best way to implement this to make sure I am submitting the actual seleted list item? Every other attempt has always grabbed the very first list item in the list no matter which submit button was clicked. I know that can't use the class:

 $(document).on('submit', '.locationForm', function(e) {
            e.preventDefault();
            // do something 
});
simlpymarkb
  • 335
  • 4
  • 11