vanilla js function to get data out of form as json code example

Example 1: FormData to json

var object = {};
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);

// Or ES6 arrow function
var object = {};
formData.forEach((value, key) => object[key] = value);
var json = JSON.stringify(object);

Example 2: get serialize data javascript

// HTML
<form id="myForm" name="myForm">
  <div>
    <label for="username">Enter name:</label>
    <input type="text" id="username" name="username">
  </div>
  <button type="button" onclick="clickMe()">Click</button>
</form>

// JS
function clickMe(){
  let myForm = document.getElementById('myForm');
  let formData = new FormData(myForm);
  console.log(formData.get('username'));
}