Jquery .on() submit event

Anton Abramov picture Anton Abramov · Aug 31, 2013 · Viewed 304k times · Source

I've got a problem with .on(). I have multiple form-elements (forms with class="remember"), also I add another one form.remember using AJAX. So, I want it to handle submit event something like:

$('form.remember').on('submit',function(){...}) 

but form added with AJAX doesn't work with it.

Where is the problem? Is it a bug?

Answer

Dipesh Parmar picture Dipesh Parmar · Aug 31, 2013

You need to delegate event to the document level

$(document).on('submit','form.remember',function(){
   // code
});

$('form.remember').on('submit' work same as $('form.remember').submit( but when you use $(document).on('submit','form.remember' then it will also work for the DOM added later.