diff --git a/snippets/serializeForm.md b/snippets/serializeForm.md new file mode 100644 index 000000000..4553ff858 --- /dev/null +++ b/snippets/serializeForm.md @@ -0,0 +1,25 @@ +### serializeForm + +Serializes a form into an object. + +First we transform the `form` into `FormData`, then we convert it into an `array` and from the `array` we collect an `object` + +```js +const serializeForm = form => + Array.from(new FormData(form)) + .reduce((acc, [key, value]) => ({ + ...acc, + [key]: value, + }), {}) +``` + +```html +
+``` + +```js +serializeForm(document.querySelector('#form')) // { email: 'test@email.com', name: 'Test Name' } +``` \ No newline at end of file diff --git a/tag_database b/tag_database index 7d248af61..109dbd265 100644 --- a/tag_database +++ b/tag_database @@ -264,6 +264,7 @@ sampleSize:array,random,intermediate scrollToTop:browser,intermediate sdbm:math,utility,intermediate serializeCookie:utility,string,intermediate +serializeForm:browser,beginner,utility setStyle:browser,beginner shallowClone:object,beginner shank:array,intermediate