Skip to content

useAjax(form)

The useAjax() function initializes an AJAX event controller for a form. It provides a clean, event-driven interface for handling submission success and failure events.

📥 Import

js
import { useAjax } from 'suriform/tools'

▶️ Usage

js
const form = document.querySelector('form')
const ajax = useAjax(form)

💡 Each form initializes one useAjax() controller, even if called multiple times.

🧩 Hooks

🔹 Listen for AJAX call.

js
ajax.onSuccess((response) => {
  console.log('Form submitted successfully:', response)
})

ajax.onError((error) => {
  console.error('Form submission failed:', error)
})

🧾 API

useAjax(form)

ParameterTypeRequiredDescription
formHTMLFormElementYesThe form element to attach hooks.

⚠️ Throws an Error if the argument is not a valid <form> element.

↩️ Returns

An object exposing reactive event hooks:

MethodDescription
onSuccess(callback)Triggered when an AJAX submission succeeds.
onError(callback)Triggered when an AJAX submission fails.

Released under the MIT License.