Encodes a set of form elements as a query string.
FormData
constructor to convert the HTML form
to FormData
.Array.from()
to convert to an array, passing a map function as the second argument.Array.prototype.map()
and encodeURIComponent()
to encode each field's value.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
JavaScript, Browser
Encodes a set of form elements as an object
.
JavaScript, Browser
Serializes a cookie name-value pair into a Set-Cookie header string.
JavaScript, Browser
Finds the distance from a given element to the top of the document.