HTML onformdata Event Attribute
Description
The onformdata attribute is an event handler in HTML that is triggered when a FormData object is constructed from a form, typically via the formdata event. This event allows developers to interact with the form’s data before it is sent to the server.
Key Points:
- Event Type:
formdata - Event Target: Usually a
<form>element. - Supported Forms: Only triggers for forms that are submitted using the
submit()method or by clicking a submit button that doesn’t haveformnovalidate. - Typical Use Case: You can manipulate or inspect form data programmatically before it is sent, such as adding additional fields, logging data, or performing conditional changes.
Syntax
Inline HTML:
<form onformdata="myFunction(event)">
...
</form>
JavaScript:
function myFunction(event) {
const formData = event.formData; // The FormData object
for (let [name, value] of formData) {
console.log(`${name}: ${value}`);
}
}
Or using addEventListener:
const form = document.querySelector('form');
form.addEventListener('formdata', (event) => {
const formData = event.formData;
formData.append('extraField', 'extraValue'); // Add additional data
console.log([...formData.entries()]);
});
How It Works
When a form is submitted, the browser constructs a
FormDataobject containing all form input values (name/value pairs).The
formdataevent is fired after theFormDataobject is created but before the data is sent to the server.Using the
onformdataattribute orform.addEventListener('formdata', ...), you can:- Inspect values.
- Append extra fields.
- Modify values.
The changes you make in the
FormDataobject will be included when the form is submitted.
Example
<form id="myForm" onformdata="handleFormData(event)">
<input type="text" name="username" value="John">
<input type="email" name="email" value="john@example.com">
<button type="submit">Submit</button>
</form>
<script>
function handleFormData(event) {
const formData = event.formData;
// Log all form data
for (let [key, value] of formData) {
console.log(key, value);
}
// Add an extra field dynamically
formData.append('timestamp', Date.now());
}
</script>
Behavior:
- When the form is submitted, the
handleFormDatafunction logs the form data and adds atimestampfield. - This additional field will be sent along with the other form fields to the server.
Use Cases
- Adding hidden data automatically before sending.
- Logging or auditing form submissions.
- Modifying form fields programmatically.
- Integrating with JavaScript frameworks that need to manipulate form submission data.
Syntax
<form onformdata="script(event)"></form>
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML onformdata event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
This event attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 27th December 2025
