Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent submit of a form that is loaded via ajax [duplicate]

I have a jquery ajax call that load a form inside a div and I want to prevent submit of this form but if I use jquery submit function with

$("form").submit(function(e){
  e.preventDefault(e);
  alert('submit intercept');
});

It does not work if I place in a external js file but works if I put in a script tag after the form that loaded via ajax call.

like image 918
danny36 Avatar asked Nov 08 '22 08:11

danny36


1 Answers

You can use $(document) selector and on() method for elements that are dynamically generated:

$(document).on('submit', 'form', function(e) {
    e.preventDefault();
    alert('submit intercept');
});
like image 143
SilverSurfer Avatar answered Nov 15 '22 11:11

SilverSurfer