Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,26 @@
---
title: Serialize form
type: snippet
tags: [browser,string]
cover: down-the-stream
dateModified: 2020-10-22T20:24:30+03:00
---
Encodes a set of form elements as a query string.
- Use the `FormData` constructor to convert the HTML `form` to `FormData`.
- Use `Array.from()` to convert to an array, passing a map function as the second argument.
- Use `Array.prototype.map()` and `encodeURIComponent()` to encode each field's value.
- Use `Array.prototype.join()` with appropriate arguments to produce an appropriate query string.
```js
const serializeForm = form =>
Array.from(new FormData(form), field =>
field.map(encodeURIComponent).join('=')
).join('&');
```
```js
serializeForm(document.querySelector('#form'));
// email=test%40email.com&name=Test%20Name
```