JavaScript, Browser, String

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 and encodeURIComponent() 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 =>'=')

Recommended snippets

  • formToObject

    JavaScript, Browser

    Encodes a set of form elements as an object.

  • serializeCookie

    JavaScript, Browser

    Serializes a cookie name-value pair into a Set-Cookie header string.

  • createElement

    JavaScript, Browser

    Creates an element from a string (without appending it to the document). If the given string contains multiple elements, only the first one will be returned.