I’m using js to add form elements into the html document. For instance, I’m adding an entire <form> to a table cell. Eg:
<form class="add-comment"><input type="submit" /></form>
I try to use js to invoke events based on elements within that newly created form but nothing happens. I should get an alert box. What am I doing wrongly?
$('.add-comment').submit(function() {
alert('form submitted!');
});
Thanks.
Hope I explained it well enough.
Are you adding the element after you register the submit event handler? You will need to register the event after each new element, unless you use the live method to add the event.
This adds the event for all selected elements, even if they are added in the future.
http://api.jquery.com/live/