[javascript] form serialize javascript (no framework)

Works in all browsers.

_x000D_
_x000D_
const formSerialize = formElement => {_x000D_
  const values = {};_x000D_
  const inputs = formElement.elements;_x000D_
_x000D_
  for (let i = 0; i < inputs.length; i++) {_x000D_
    values[inputs[i].name] = inputs[i].value;_x000D_
  }_x000D_
  return values;_x000D_
}_x000D_
_x000D_
const dumpValues = form => () => {_x000D_
  _x000D_
  const r = formSerialize(form);_x000D_
  console.log(r);_x000D_
  console.log(JSON.stringify(r));_x000D_
}_x000D_
_x000D_
const form = document.querySelector('form');_x000D_
_x000D_
dumpValues(form)();_x000D_
_x000D_
form.addEventListener('change',dumpValues(form));
_x000D_
<form action="/my-handling-form-page" method="post">_x000D_
  <div>_x000D_
    <label for="name">Name:</label>_x000D_
    <input type="text" id="name" name="user_name" value="John">_x000D_
  </div>_x000D_
  <div>_x000D_
    <label for="mail">E-mail:</label>_x000D_
    <input type="email" id="mail" name="user_mail" value="[email protected]">_x000D_
  </div>_x000D_
  <div>_x000D_
    <label for="interests">Interest:</label>_x000D_
    <select required=""  id="interests" name="interests">_x000D_
      <option value="" selected="selected">- None -</option>_x000D_
      <option value="drums">Drums</option>_x000D_
      <option value="js">Javascript</option>_x000D_
      <option value="sports">Sports</option>_x000D_
      <option value="trekking">Trekking</option>_x000D_
    </select>_x000D_
  </div>_x000D_
  <div>_x000D_
    <label for="msg">Message:</label>_x000D_
    <textarea id="msg" name="user_message">Hello My Friend</textarea>_x000D_
  </div>_x000D_
</form>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to serialization

laravel Unable to prepare route ... for serialization. Uses Closure TypeError: Object of type 'bytes' is not JSON serializable Best way to save a trained model in PyTorch? Convert Dictionary to JSON in Swift Java: JSON -> Protobuf & back conversion Understanding passport serialize deserialize How to generate serial version UID in Intellij Parcelable encountered IOException writing serializable object getactivity() Task not serializable: java.io.NotSerializableException when calling function outside closure only on classes not objects Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly