After edit of OP
You do not need to preventDefault of the click.... only the submit... here is you working code:
jQuery(document).ready(function(){
$('form.example').submit(function(event){
event.preventDefault();
alert("form submitted");
// stop submission so we don't leave this page
});
$('form.example button').click(function() {
alert("button clicked");
});
});
old answer
You can simply put your .click()
and .submit()
handlers in series, and they should not cancel out. You have some syntax errors in your pseudo code.... maybe those are causing problems?
Another potential problem is that $("form button")
targets the HTML <button>
tags. If you use <input type="button" />
you should use $("form:button")
and note that <input type="submit" />
is not a button. Anyway, I'll assume you are in fact using the <button>
tags.
Usually return false
is used inside .submit(function() { ... });
. This stops the form from being submited through HTML. s**[topPropagation][6]**
is very different. It deals with stopping events "bubbling up" to the parents of elements....... But I don't see how this would effect your case.
If you are doing a true HTML submission, make sure to put your .click()
handler first, since a true HTML submission will cause you to leave the page.
If you use return false
inside .submit()
, the form will not be submitted through the HTML, and you'll have to handle the submission with jQuery / Javascript / AJAX.
Anyway, here is a demonstration of both the .click()
and .submit()
events firing in series... the code is below:
$(function() {
$('form button').click(function() {
// Do click button stuff here.
});
$('form').submit(function(){
// Do for submission stuff here
// ...
// stop submission so we don't leave this page
// Leave this line out, if you do want to leave
// the page and submit the form, but then the results of your
// click event will probably be hard for the user to see.
return false;
});
});
The above will trigger both handlers with the following HTML:
<button type="submit">Submit</button>
As a note, I suppose you were using pseudo code, but even then, it's much easier to read, and one is sure you're not writing syntax errors, if you use:
$('form').submit(function() { /*submits form*/ });
$('form button').click(function() { /*does some action*/ });