Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active January 11, 2021 15:15
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 kieranbarker/245182a6e6af0b05be4233881c0aa60b to your computer and use it in GitHub Desktop.
Save kieranbarker/245182a6e6af0b05be4233881c0aa60b to your computer and use it in GitHub Desktop.
Serialize all form data into an object
/**
* Serialize all form data into an object
* {@link https://gist.github.com/kieranbarker/245182a6e6af0b05be4233881c0aa60b}
* @param {HTMLFormElement} form The form to serialize
* @returns {Object} The serialized form data
*/
function serializeObject (form) {
// Create a new FormData object
const formData = new FormData(form);
// Create an object to hold the name/value pairs
const pairs = {};
// Add each name/value pair to the object
for (const [name, value] of formData) {
pairs[name] = value;
}
// Return the object
return pairs;
}
@kieranbarker
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment