Present events, like other events, bubbles in the DOM, so working with the send event listener for the form itself or any of its parents will work, and you can even cancel your application there.
To always get the form in which the request was submitted, use e.target instead of this in the event listener method.
See this example:
function onSubmit(e){ e.preventDefault(); document.getElementById('d3').innerHTML += 'Submit catched in ' + e.currentTarget.tagName + '#' + e.currentTarget.id + ' for ' + e.target.tagName + '#' + e.target.id + '<br/>'; } $(function(){ $('#d1').on('submit', onSubmit); $('#f2').on('submit', onSubmit); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="d1"> <form id="f1"> <input type="submit" value="Event Listener on DIV" /> </form> </div> <div id="d2"> <form id="f2"> <input type="submit" value="Event Listener on Form" /> </form> </div> <div id="d3"> </div>
By launching it and clicking on the buttons, you can see that with the same on call you can attach the evest to the form itself or its parent element, and it will work as expected.
So, you can only use the line below, and it will work fine:
$(containerSelectorOrjQueryObject).on("submit", submit);
source share