DEPRECATED - this part is outdated so please don't use it.
You can also try this code, if you have for example later added dynamic forms. For example you loaded a window async with ajax and want to submit this form.
$('#cpa-form').live('submit' ,function(e){
e.preventDefault();
// do something
});
UPDATE - you should use the jQuery on() method an try to listen to the document DOM if you want to handle dynamically added content.
Case 1, static version: If you have only a few listeners and your form to handle is hardcoded, then you can listen directly on "document level". I wouldn't use the listeners on document level but I would try to go deeper in the doom tree because it could lead to performance issues (depends on the size of your website and your content)
$('form#formToHandle').on('submit'...
OR
$('form#formToHandle').submit(function(e) {
e.preventDefault();
// do something
});
Case 2, dynamic version: If you already listen to the document in your code, then this way would be good for you. This will also work for code that was added later via DOM or dynamic with AJAX.
$(document).on('submit','form#formToHandle',function(){
// do something like e.preventDefault();
});
OR
$(document).ready(function() {
console.log( "Ready, Document loaded!" );
// all your other code listening to the document to load
$("#formToHandle").on("submit", function(){
// do something
})
});
OR
$(function() { // <- this is shorthand version
console.log( "Ready, Document loaded!" );
// all your other code listening to the document to load
$("#formToHandle").on("submit", function(){
// do something
})
});