Skip to content

Configuration

Pass one options object to formsub.newForm(options).

Required

OptionTypeDescription
formIdstringForm element id (not a selector).
submitButtonstringCSS selector for the submit control.
ajaxUrlstringURL passed to fetch.

If any of these are missing, or the form / button is not in the DOM, newForm logs an error and returns without binding.

Optional

OptionTypeDefaultDescription
methodstring'POST'HTTP method. No body on GET / HEAD.
dataobjectnullExtra key/value pairs appended to FormData.
indicatorstringnullCSS selector for a loading element.
recaptchaboolean | stringnullEnable reCAPTCHA when truthy and not "false".
recaptcha_site_keystringnullSite key for grecaptcha.execute.
itifalse | objectfalseintl-tel-input setup.
responseType'default' | 'swal''default'Inline errors, or SweetAlert2.
callbackfunctionnullReplaces the default response handler.

Full example

js
formsub.newForm({
  formId: 'contact-form',
  submitButton: '#submit-button',
  ajaxUrl: '/api/contact',
  method: 'POST',
  data: { source: 'homepage', campaign: 'spring' },
  indicator: '#loading',
  recaptcha: true,
  recaptcha_site_key: 'YOUR_SITE_KEY',
  iti: { id: 'phone', country: 'BG' },
  responseType: 'swal',
});

Extra fields (data)

Static values appended to FormData after the visible form fields:

js
data: {
  source: 'homepage',
  userId: 42,
}

Notes:

  • Keys become field names on the server.
  • Prefer strings or numbers. Nested objects are not flattened.
  • If a key already exists on the form, both values are sent.
  • data is captured when you call newForm. For values that change on every submit, append them in a formsub:BeforeSubmit listener instead.

Loading indicator

html
<p id="loading" class="hidden">Sending…</p>
js
indicator: '#loading'

While the request is in flight, formsub:

  • Adds disabled on the submit button
  • Removes hidden and uk-hidden on the indicator

Both are restored in a finally block, including cancelled submits and client validation failures that return early.

Released under the MIT License.