Skip to content

Instantly share code, notes, and snippets.

@daniel12fsp
Created March 31, 2020 13:01
Show Gist options
  • Save daniel12fsp/5409ae13fd30e2467f598804e2767e19 to your computer and use it in GitHub Desktop.
Save daniel12fsp/5409ae13fd30e2467f598804e2767e19 to your computer and use it in GitHub Desktop.
serializer_form_to_json
// Function rule copied from https://api.jquery.com/serializeArray implementation
form = document.querySelector("form")
rselectTextarea = /^(?:select|textarea)/i;
rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i
Array.from(form.elements).filter(function(elem){
return elem.name && !elem.disabled &&
( elem.checked || rselectTextarea.test( elem.nodeName ) ||
rinput.test( elem.type ) );
}).reduce((obj, currentValue) => ({...obj, [currentValue.name]:obj[currentValue.name]? [obj[currentValue.name], currentValue.value]: currentValue.value }))
//Reset form.reset()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment