tags:

views:

58

answers:

1

"return false" seems to work in the submitHandler but I'm not sure why.

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

$(document).ready(function () {

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

    $("#tip_click").click(function() {
        $("#high_hat").submit(submitHandler);
    });

});

I'm also not sure if I need to add a "return false" in the #tip_click function so that it reads like this:

    $("#tip_click").click(function() {
        $("#high_hat").submit(submitHandler);
        return false;
    });

What's the rule to follow here regarding when to "return false"?

+4  A: 

You should return false when:

  • You want to prevent the default event action

    • The onsubmit event for example, the default action of this event is send the form data to the server, or an onclick event on an anchor element, the default event action is to follow the anchor's HREF.
  • You want to stop the event bubbling.

Returning false from an event handler, is like executing both event.preventDefault(); and event.stopPropagation();.

CMS