At first I was trying to use .submit()
but read that it won't work with dynamically generated elements (after the DOM is ready). So I tried .on()
. It works in one script, but I'm having trouble getting it to work with button clicks.
The forms are placed dynamically. I'm using bootstrap-tables to load JSON into a table, an the form is part of the JSON that gets passed. There are up to 100 forms on each page.
Here's my form and the table structure it's in:
<table id="report-table">
<tbody>
<tr>
<td></td><td></td><td></td>
<td>
<form>
<input type="text" />
<button type="submit" id="add_tag">Add Tag</button>
</form>
</td>
</tr>
</tbody>
</table>
I've tried:
//I'm assuming this didn't work because the form was generated using JS
$(".add_tag").click( function(e) {
e.preventDefault();
alert("button clicked");
});
//Same reasoning as above
$("form").submit( function(e) {
e.preventDefault();
alert("button clicked");
});
//Not sure why this worn't work
$( "#report-table tbody tr td form button" ).on( "click", function(e) {
e.preventDefault(); //this doesn't even work.
alert("button clicked");
});
Any ideas?