Re is out: see what's new Lifetime updates on every purchase Support replies within 24 hours Secure checkout via Gumroad 8,500+ sales since 2016

JavaScript events

Updated 2 min read 6 views

Re:Maily’s front-end script fires events on the <form> element. They bubble, so listen on document. Each has event.detail with id (the form’s post ID), form and response (the server’s reply, when there is one).

EventWhen
remaily:submitBefore sending. Call event.preventDefault() to stop it.
remaily:sentAfter any reply
remaily:successThe message went through
remaily:invalidSome fields need fixing
remaily:failedThe mail couldn’t be sent
remaily:spamThe message was caught as spam
remaily:abortedA hook on the server stopped the message
remaily:paymentThe fields are fine and a Stripe or PayPal payment comes first. The payment box opens; once paid, the form is sent again.
remaily:resetThe form was cleared, after sending or by a reset button
remaily:readyThe form is set up and ready to use, once per form
document.addEventListener( 'remaily:success', ( event ) => {
    console.log( 'Form', event.detail.id, 'sent' );
} );

Forms added later

Forms loaded into a popup or by AJAX after the page loads need one call to start working:

window.remaily.init( popupElement );

A step before sending

Push an async function to window.remaily.beforeSend. Re:Maily waits for it before sending, which is how the reCAPTCHA token is fetched.

window.remaily.beforeSend.push( async ( form ) => {
    // await something, then let the form go
} );

Check part of a form

window.remaily.check( element ) checks the fields inside an element of a form, such as one step of a multi-step form. It shows their errors, moves the focus to the first one and returns true when all of them pass. Pass the form to check all of it.

nextButton.addEventListener( 'click', () => {
    if ( window.remaily.check( currentStep ) ) {
        showNextStep();
    }
} );

Your own check

Push a function to window.remaily.checks to check fields the browser can’t, such as a signature. It gets each field’s wrapper (data-remaily-field, data-remaily-type) and returns '' when the field is fine, or a message: a key from the Messages tab, or your own text. It runs when the form is sent and while the visitor fixes a field. Check the same thing on the server with remaily_validate_{type}.

window.remaily.checks.push( ( wrap ) => {
    if ( wrap.dataset.remailyType !== 'signature' ) {
        return '';
    }

    return wrap.querySelector( 'input' ).value ? '' : 'Please sign the form.';
} );
Was this article helpful?