form serialize javascript (no framework)

RussellHarrower picture RussellHarrower · Jul 26, 2012 · Viewed 178.2k times · Source

Wondering is there a function in javascript without jquery or any framework that allows me to serialize the form and access the serialized version?

Answer

Artur Beljajev picture Artur Beljajev · Oct 22, 2014

Here is pure JavaScript approach:

var form = document.querySelector('form');
var data = new FormData(form);
var req = new XMLHttpRequest();
req.send(data);

Though it seems to be working only for POST requests.

https://developer.mozilla.org/en-US/docs/Web/API/FormData