Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Last active September 7, 2022 18:56
Show Gist options
  • Save WesleySmits/0be8b06aaecfa16007256d7c44ff74b3 to your computer and use it in GitHub Desktop.
Save WesleySmits/0be8b06aaecfa16007256d7c44ff74b3 to your computer and use it in GitHub Desktop.
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