Skip to content

Instantly share code, notes, and snippets.

@askkaz

askkaz/before.js Secret

Created April 29, 2022 15:00

Revisions

  1. askkaz created this gist Apr 29, 2022.
    35 changes: 35 additions & 0 deletions before.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <script type="text/javascript">
    /* apply only to forms with the action pointing to Loops */
    $('form[action^="https://app.loops.so"]').each(function (i, el) {
    form = $(el);
    form.submit(function (e) {
    /* stop the form from submitting */
    e.preventDefault();
    form = $(e.target);
    action = form.attr('action');
    /* submit the form via ajax */
    $.ajax({
    url: action,
    method: "POST",
    data: form.serialize(),
    dataType: "json",
    success: function (data) {
    if (data.success) {
    /* successful submission - hide the form and show the success message */
    parent = $(form.parent());
    parent.children('form').css('display', 'none');
    parent.children('.w-form-done').css('display', 'block');
    } else {
    parent = $(form.parent());
    parent.find('.w-form-fail').css('display', 'block');
    }
    },
    error: function () {
    /* failed submission - show the failure message */
    parent = $(form.parent());
    parent.find('.w-form-fail').css('display', 'block');
    }
    });
    });
    });
    </script>