views:

100

answers:

4

*Update: I have pasted working code in order to erase any ambiguity about what is going on. I have also tried to remove the preventDefault on both handlers, does not help

I have a form where upon the button click, a JS event needs to happen, and the form needs to submit.

As per the code below, what I thought would happen is: alert(button), then alert(form), or vice versa. I do not care about sequence.

If i run it however, the alert(button) will show up, but the alert(form) will not.

If i comment out the code for the button, the form alert comes up.

Do i have some fundamental misunderstanding of how this is supposed to work?

jQuery(document).ready(function(){

  $("form.example").submit(function(event){
    event.preventDefault();
    alert("form submitted");
  });


  $("form.example button").click(function(event){
    event.preventDefault();
    alert("button clicked");
  });
)};


<form class="example" action="/v4test">
  <button type="submit">Meow!</button>  
</form>
A: 

If you put a return false on the click, it should cancel the default behavior. If you want to execute one then the other, call $('form').submit() within the click function. e.g.

$('form').submit { //submits form}

$('form button').click {
// does some action
$('form').submit();
}
Gazler
This code is formatted badly, what is `$('form').submit {` and what is `$('form button').click {` ? This code will not work at all.
Peter Ajtai
They are copied from the additional example.
Gazler
@Gazler - The example is wrong and won't work. This may be one of the reasons the OP is asking the question in the first place.
Peter Ajtai
Hi folks! Peter is right i do not want it to execute one after the other, because the button click is a pure client action, while the form submit is a ajax action => so i can have both execute at the same time. Secondly, the button click behavior is universal, which means it can affect many forms/scenarios. So i do not want to tie it down un-neccessarily to any given actions
ming yeow
btw, the example is just a pseduo code, i will take more care to write proper code =)
ming yeow
Ahh, I misunderstood the question then. Apologies.
Gazler
A: 

Does clicking the button submit the form? If so:

// Disable the submit action
$("form").submit(function(){
    return false; 
});

$("form button").click(function(){
    // Do some action here

    $("form").unbind("submit").submit();

});

If you don't unbind the submit event when you click the button, the submit will just do nothing.

SimpleCoder
This code is formated quite badly. It will not work at all. What is `$("form").submit(){ return false; }`?
Peter Ajtai
@Peter Ajtai; My apologies, I meant to do `$("form").submit(function(){ return false; });`
SimpleCoder
+2  A: 

After edit of OP

You do not need to preventDefault of the click.... only the submit... here is you working code:

jsFiddle example


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:

jsFiddle Example


$(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*/ });
Peter Ajtai
`return false` is quite different from `stopPropagation`, see my answer for details.
Tgr
@Tgr - Which is why I wrote, `Usually return false is used inside .submit(function() { ... }); not stopPropagation which deals with even bubbling`... maybe I wasn't clear in my phrasing.
Peter Ajtai
Hi Peter! I have pasted actual working code above. I understand your code, and I appreciate your comments, but it does not quite solve the basic problem here. Thanks a lot, and sorry for the lousy pseudo code!
ming yeow
@ming - Edited. Take a look. You only need one of the preventDefaults.
Peter Ajtai
@peter thanks! do you mind explaining why this works? thanks a lot for your help, i am still getting around how the async works exactly
ming yeow
@ming - You only need to cancel the submital of the form with preventDefault. The click on the button itself doesn't need to be cancelled, since that is separate from the submit.
Peter Ajtai
@peter - i see - the form was prevented from submitting BECAUSE of the preventdefault of the button. how obvious can it get. =)
ming yeow
A: 

There seems to be a bit of confusion about propagation here. Event propagation (which can be disabled by stopPropagation) means that events "bubble up" to parent elements; in this case, the click event would register on the form, because it is a parent of the submit button. But of course the submit handler on the form will not catch the click event.

What you are interested in is the default action, which in the case of clicking a submit button is to submit the form. The default action can be prevented by either calling preventDefault or returning false. You are probably doing the latter.

Note that in Javascript functions which do not end with an explicit return do still return a value, which is the result of the last command in the function. You should end your click handler with return; or return true;. I have no idea where I got that from. Javascript functions actually return undefined when there is no explicit return statement.

Tgr
Hi Tgr, as mentioned to Peter, I am not really talking about the propagation problem per se, i am more confused because these 2 events are supposed to fire independently (i do not care about the sequence.)
ming yeow
They are not at all independent. As I tried to explain, the default action of the click event is submitting the form, which causes the submit event. Since you prevent the default action, there is no reason for a submit event to happen.
Tgr