Skip to content

Instantly share code, notes, and snippets.

@jamesmurdza
Created March 29, 2024 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmurdza/3cc584240ec40c3d257a3b8be4773145 to your computer and use it in GitHub Desktop.
Save jamesmurdza/3cc584240ec40c3d257a3b8be4773145 to your computer and use it in GitHub Desktop.
// Select all forms on the page
const forms = document.querySelectorAll('form');
// Iterate over each form and attach a submit event listener
forms.forEach(form => {
form.addEventListener('submit', event => {
event.preventDefault(); // Prevent the default form submission behavior
// Get form data
const formData = new FormData(form);
// Convert FormData to object
const formObject = {};
formData.forEach((value, key) => {
formObject[key] = value;
});
// Log the form data
console.log('Form Data:', formObject);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment