Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Created September 7, 2022 19:10
Show Gist options
  • Save WesleySmits/40dccfe3671e57f87d1831758654bf24 to your computer and use it in GitHub Desktop.
Save WesleySmits/40dccfe3671e57f87d1831758654bf24 to your computer and use it in GitHub Desktop.
FormData: The FormData event
const newsletterForm = document.getElementById('newsletter-form');
newsletterForm.addEventListener('submit', () => {
new FormData(newsletterForm);
});
newsletterForm.addEventListener('formdata', (event) => {
const formData = event.formData;
fetch('/some/api/endpoint', {
method: 'post',
body: formData
})
.then((response) => response.json())
.then((result) => {
console.log('Success:', result);
})
.catch((error) => {
console.error('Error:', error);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment