Skip to content
Lazyform
Docs menu

Developers

Drop-in widget

One script tag makes any HTML form submit in place with success and error messages, adds multi-step pages and partial saving, or draws a ready contact form.

The widget is a small script (about 4 KB gzipped) for sites without a build step: plain HTML, Webflow, Framer, Carrd, WordPress, Hugo, Jekyll. It defines one global, window.Lazyform, and nothing else.

html
<script src="https://lazyform.dev/widget.js" defer></script>

It finds forms on its own, including forms added later by a single-page app.

Enhance your own form

Add data-lazyform-key to any form. No action or hidden access_key field needed.

html
<form data-lazyform-key="YOUR_ACCESS_KEY">
  <input type="checkbox" name="botcheck" style="display:none" tabindex="-1" autocomplete="off">
  <label for="email">Email</label>
  <input id="email" type="email" name="email" required>
  <label for="message">Message</label>
  <textarea id="message" name="message" required></textarea>
  <button type="submit">Send</button>
</form>

On submit the widget:

  1. sends the form without leaving the page (with the clicked button's value, and the _lf_started field for the time trap);
  2. disables the submit button and shows "Sending…";
  3. on success clears the form and shows "Thanks! Your message has been sent.";
  4. on error shows Lazyform's explanation (for example "Please enter a valid email address.") and keeps what the visitor typed.

Messages appear in a <p class="lazyform-status" role="status"> added at the end of the form. To place it yourself, add an empty element with data-lazyform-status anywhere inside the form.

Attributes

Attribute on the form Effect
data-lazyform-key The form's access key (required)
data-lazyform-success Success message
data-lazyform-error Message for every error, instead of Lazyform's explanation
data-lazyform-sending Text while sending
data-lazyform-network-error Message when the visitor is offline
data-lazyform-redirect Go to this URL after success instead of showing a message (a redirect field works too)
data-lazyform-partial Save what visitors type before submitting (below)
data-lazyform-endpoint Post somewhere else (testing)

Every text the widget shows can be replaced this way, which also translates it: data-lazyform-next, data-lazyform-back, data-lazyform-step-progress ("Step {current} of {total}"), data-lazyform-submit, data-lazyform-label-name, data-lazyform-label-email, data-lazyform-label-message.

Events

The form fires lazyform:success (event.detail is { id, message }) and lazyform:error ({ code, message, status }). Both bubble. Call event.preventDefault() to show your own message instead:

html
<script>
  document.addEventListener('lazyform:success', (event) => {
    event.preventDefault()
    event.target.outerHTML = '<p>Thanks! See you soon.</p>'
  })
</script>

Styling

Status messages carry data-state="sending" | "success" | "error", and the form gets aria-busy="true" while sending, so you can style them:

css
.lazyform-status[data-state='error'] { color: crimson; }
form[aria-busy='true'] { opacity: 0.7; }

A ready contact form

No form at all? Put a div where it should appear:

html
<script src="https://lazyform.dev/widget.js" defer></script>
<div data-lazyform-form="YOUR_ACCESS_KEY"></div>

The widget draws a labelled, keyboard-friendly form with Name, Email (required), Message (required), a honeypot and a submit button. Anything inside the div (like a "Loading…" text or a <noscript> link) is replaced.

Every data-lazyform-* attribute from the list above also works on the div, plus data-lazyform-subject for the email subject:

html
<div data-lazyform-form="YOUR_ACCESS_KEY"
     data-lazyform-subject="New enquiry from the website"
     data-lazyform-label-message="How can we help?"
     data-lazyform-submit="Send enquiry"
     data-lazyform-success="Thanks! We reply within a day."></div>

The styles have no specificity (they use :where()), so your site's CSS always wins. Change the look with CSS variables:

css
.lazyform {
  --lazyform-accent: #4f46e5;      /* button and focus ring */
  --lazyform-accent-text: #fff;    /* button text */
  --lazyform-radius: 12px;
  --lazyform-border: #d4d4d8;
}

Multi-step forms

Split a form into pages by wrapping each page in data-lazyform-step. The visitor sees one page at a time; Next checks the page's required fields before moving on, and the whole form is sent once from the last page.

html
<form data-lazyform-key="YOUR_ACCESS_KEY">
  <p data-lazyform-progress></p>

  <section data-lazyform-step>
    <label for="name">Name</label>
    <input id="name" name="name" required>
  </section>

  <section data-lazyform-step>
    <label for="budget">Budget</label>
    <select id="budget" name="budget" required>
      <option value="">Choose…</option>
      <option>Under $5k</option>
      <option>$5k or more</option>
    </select>
  </section>

  <section data-lazyform-step>
    <label for="details">Project details</label>
    <textarea id="details" name="details"></textarea>
    <button type="submit">Send</button>
  </section>
</form>
  • Pages without buttons get Next and Back added. To place or style them yourself, add <button type="button" data-lazyform-next> or data-lazyform-back inside the page.
  • Pressing Enter moves to the next page instead of sending.
  • data-lazyform-progress elements show "Step 2 of 3". The form's data-lazyform-current-step attribute holds the page number, for styling.
  • After a successful submission the form goes back to the first page.

Partial submissions

Add data-lazyform-partial to save what visitors type, even if they never press Send. They appear with the form's submissions in the dashboard. Needs the Save partial submissions feature on the form (Business plan).

html
<form data-lazyform-key="YOUR_ACCESS_KEY" data-lazyform-partial>
  …
</form>

The widget saves a second after the visitor stops typing and when they leave the page. Files, passwords and captcha tokens are never saved. Each visit gets an id (kept for the browser tab's session, so a reload continues the same visit); when the visitor submits, the saved partial is marked as completed and disappears from the list.

JavaScript API

The script defines window.Lazyform:

Function Use
Lazyform.init(root?) Scan the page (or an element) for forms again. Usually not needed: new forms are found automatically
Lazyform.enhance(form) Enhance one form element
Lazyform.render(div) Draw the contact form into one element
Lazyform.submit(key, data, options?) Same as submit() from the npm package
Lazyform.savePartial(key, visitId, data, options?) Same as savePartial()
Lazyform.version The widget's version

Self-hosting and CDNs

https://lazyform.dev/widget.js always sends to the Lazyform it was loaded from. The copy in the npm package (lazyform/widget.js, also on jsDelivr and unpkg) sends to https://api.lazyform.dev/submit. To send elsewhere, add data-endpoint to the script tag:

html
<script src="/js/widget.js" data-endpoint="https://api.lazyform.dev/submit" defer></script>

If your site has a strict Content Security Policy, allow the script's origin in script-src, the API in connect-src, and inline styles (style-src 'unsafe-inline') for the ready-made form's styles.