Kam
Kam

Reputation: 6008

js generated form

I have a js generated form (i.e. I echo a form from js code).

So I wrote a submit handler that is activated when that form is submitted using:

$(document).ready(function(){
$('#form-coures').submit(function(event){

At least this is what I want to achieve.

The issue here is that when I submit that form the handler isn't called?!

Any ideas?

Note: if I define that same form not through js everything works fine.

Upvotes: 1

Views: 74

Answers (3)

pdoherty926
pdoherty926

Reputation: 10349

The form doesn't exist when you're attaching the event handler.

Use event delegation $(document).on('submit', 'form', function () {...})

Upvotes: 0

Bort
Bort

Reputation: 7618

It's hard to say without actually seeing all of your code, but you register the submit event handler on document ready, at which time the DOM element form-courses wouldn't exist yet (if you are generating it after page load using javascript). Register the event after you add the form to the page.

Upvotes: 2

MaxOvrdrv
MaxOvrdrv

Reputation: 1916

well... i've never done this with JQuery (e.g.: make an entire form programmatically), but as far as ideas, have you thought about hokking an onclick event to your submit button instead?

Upvotes: -1

Related Questions