814 B
814 B
title, tags, cover, firstSeen, lastUpdated
| title | tags | cover | firstSeen | lastUpdated |
|---|---|---|---|---|
| Serialize form | browser,string | blog_images/down-the-stream.jpg | 2019-03-13T14:29:45+02:00 | 2020-10-22T20:24:30+03:00 |
Encodes a set of form elements as a query string.
- Use the
FormDataconstructor to convert the HTMLformtoFormData. - Use
Array.from()to convert to an array, passing a map function as the second argument. - Use
Array.prototype.map()andencodeURIComponent()to encode each field's value. - Use
Array.prototype.join()with appropriate arguments to produce an appropriate query string.
const serializeForm = form =>
Array.from(new FormData(form), field =>
field.map(encodeURIComponent).join('=')
).join('&');
serializeForm(document.querySelector('#form'));
// email=test%40email.com&name=Test%20Name