Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Last active Sep 7, 2022
Embed
What would you like to do?
Formdata: Usage of the FormData object
const newsletterForm = document.getElementById('newsletter-form');
newsletterForm.addEventListener('submit', () => {
const formData = new FormData(newsletterForm);
const name = formData.get('name');
// Append timestamp
formData.append('timestamp', Date.now().toString());
// Delete timestamp
formData.delete('timestamp');
// Set name
formData.set('name', 'John Doe');
// Loop through formData
for (const [key, value] of formData) {
console.log(key, value);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment