I\'ve done a lot of searching but can\'t find out why my jQuery .submit()
will not trigger. I\'ve also tried with .click()
for the submit button.
If you're dynamically adding content, that static call to .submit()
isn't going to work for you. That will only bind the event handler to the elements that matched the selector at the time it was run, so any elements added after that won't have the event handler bound.
The solution is event delegation, using the .on() function:
$(document).on('submit', '.reply-message-form', function(e) {
alert('test');
return false;
});
Ideally, rather than using document
, you'd select the closest static (i.e. exists when the page loads) element that will contain all of your dynamic content.
Alternatively, if you're using a version of jQuery prior to 1.7 and therefore don't have access to the .on()
function, you can use the .delegate() function instead:
$(document).delegate('.reply-message-form', 'submit', function(e) {
alert('test');
return false;
});
For dynamically generated elements, you should delegate the events, you can use the on
method, try the following:
$(document).ready(function() {
$(document).on('click', 'input[type=submit]', function() {
alert('test');
return false;
});
});