![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@itrocks/form-fetch
Advanced tools
AJAX fetch for HTML form submissions using form DOM data.
npm install form-fetch
With an HTML page containing a button, a form, and a div with id result, you can fetch your form when clicking the button:
import formFetch from './node_modules/@itrocks/form-fetch/form-fetch.js'
document.querySelector('button').addEventListener('click', async () =>
{
const htmlResponse = await formFetch(document.querySelector('form')).text()
document.getElementById('#result').innerHTML = htmlResponse
})
Alternatively, add a submit event listener to the form to trigger a fetch instead of a standard submission:
import { formFetchOnSubmit } from './node_modules/@itrocks/form-fetch/form-fetch.js'
document.addEventListener('load', () => {
formFetchOnSubmit(document.querySelector('form'), response => {
document.getElementById('#result').innerHTML = htmlResponse
})
})
For a streamlined approach, use xtarget
and build to automate form-fetch
:
import { buildXTarget } from './node_modules/@itrocks/xtarget/xtarget.js'
buildXTarget()
form attributes automatically set fetch() options:
You can override these options with custom values for action and RequestInit.
A type alias for form submitter elements:
type FormElement = HTMLButtonElement | HTMLFormElement | HTMLInputElement
Submits a form using fetch(), based on form DOM data.
response = formFetch(form)
response = formFetch(form, action)
response = formFetch(form, action, init)
document.querySelectorAll('form').forEach(form => {
formFetch(form)
.then(response => response.text())
.then(html => document.getElementById('#result').append(html))
})
Attaches a submit event listener to fetch instead of submit.
formFetchSubmit(element, setResponse)
formFetchSubmit(element, setResponse, init)
setResponse(response, targetSelector, form)
Determines the effective method for fetch() submit.
method = formMethod(form)
method = formMethod(form, init)
data-method
attribute value of the form,FAQs
AJAX fetch for HTML form submissions using form DOM data
The npm package @itrocks/form-fetch receives a total of 3 weekly downloads. As such, @itrocks/form-fetch popularity was classified as not popular.
We found that @itrocks/form-fetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.