tags:

views:

35

answers:

2

When I submit #top_cat, the firebug console says "ok" because of the debug text I'm printing here:

function submitHandler() {
    console.log("ok");
    $.post($(this).attr('action'), $(this).serialize(), null, "script");
    return false;
}

But if I click on #tip_click, I see "start" and "end" in the output but it never says "ok" - meaning the submitHandler never ran.

Is there anything obviously wrong with this code?

$(document).ready(function () {

    $('#top_cat').submit(submitHandler);

    $("#tip_click").click(function() {
        console.log("start");
        $("#high_hat").submit(submitHandler);
        console.log("end");
    });

});
+2  A: 
$("#high_hat").submit(submitHandler);

This registers an onsubmit handler but does not run the handler. It sets up the handler to be called whenever #high_hat is submitted. If you want to run the submit handler right away you'll need to trigger it yourself by calling submit() (no arguments):

console.log("start");
$("high_hat").submit(submitHandler).submit();
console.log("end");
John Kugelman
+1 for what I wanted to write.
thephpdeveloper
A: 

Why not try:

$("#tip_click").click(function() {
        console.log("start");
        submitHandler();
        console.log("end);
});

I don't see any benefit to submitting it manually, since it returns false anyway, so just calling the submitHandler directly will simplify the code.

I don't see any benefit to submitting as John suggests since you return false anyway.

James Black
I was going to suggest this but he uses `$(this)` inside `submitHandler`.
John Kugelman
Oops, I missed that.
James Black