Last active
January 11, 2021 15:15
-
-
Save kieranbarker/245182a6e6af0b05be4233881c0aa60b to your computer and use it in GitHub Desktop.
Serialize all form data into an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View the demo:
https://codepen.io/kieranbarker/pen/zYKJaXO