Skip to content

Ensuring $.fn.submit is aligned with jQuery #363

@vovayatsyuk

Description

@vovayatsyuk

I'm trying to submit a form and trigger the submit event as well.

$(document).on('submit', function (event) {
    alert('submit');
});
form.trigger('submit') // event is raised, but form is not submitted
form.submit() // form is submitted, but event is not raised

To accomplish this task I tried to do the following:

$.fn.submit = function () {
    this.each(function () {
        $(this).trigger('submit');
        this.submit();
    });
};

It works as expected until I decided to use turbo. When the turbo is included on the site
and my custom fn.submit is used, the form gets submitted twice. Looks like turbo handles triggered events and submits a form on its own.

The interesting part in this thing that when using jquery's $form.submit() an event is raised and the form is submitted as well but only once. With or without turbo.

So, what magic trick does jquery do to submit the form once only? Or is it some fix in turbo code? And is there any other way to trigger an event and submit a form?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions