New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@itrocks/form-fetch

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@itrocks/form-fetch

AJAX fetch for HTML form submissions using form DOM data

  • 0.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-83.33%
Maintainers
0
Weekly downloads
 
Created
Source

npm version npm downloads GitHub issues discord

form-fetch

AJAX fetch for HTML form submissions using form DOM data.

Installation

npm install form-fetch

Usage

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()

Features

form attributes automatically set fetch() options:

You can override these options with custom values for action and RequestInit.

API

FormElement

A type alias for form submitter elements:

type FormElement = HTMLButtonElement | HTMLFormElement | HTMLInputElement

formFetch()

Submits a form using fetch(), based on form DOM data.

response = formFetch(form)
response = formFetch(form, action)
response = formFetch(form, action, init)
Parameters
Example
document.querySelectorAll('form').forEach(form => {
	formFetch(form)
		.then(response => response.text())
		.then(html => document.getElementById('#result').append(html))
})

formFetchOnSubmit()

Attaches a submit event listener to fetch instead of submit.

formFetchSubmit(element, setResponse)
formFetchSubmit(element, setResponse, init)
Parameters
  • element: A FormElement (form, button, or input).
  • setResponse: A callback handling the Response:
    setResponse(response, targetSelector, form)
    
    • response: The retrieved Response.
    • targetSelector: The selector for the target where the form response should be displayed, determined by the formtarget of the submitter or the target of the form.
    • form: The submitted HTMLFormElement.

  • init: A callback that returns a RequestInit object containing any custom settings to apply to the request.

formMethod()

Determines the effective method for fetch() submit.

method = formMethod(form)
method = formMethod(form, init)
Parameters

Keywords

FAQs

Package last updated on 20 Dec 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc