Skip to content

Instantly share code, notes, and snippets.

@AleksejDix
Created March 6, 2018 12:19
Show Gist options
  • Save AleksejDix/de342e46c7fc5402d49f176d2ecc445a to your computer and use it in GitHub Desktop.
Save AleksejDix/de342e46c7fc5402d49f176d2ecc445a to your computer and use it in GitHub Desktop.
const serialize = (form) => {
if (typeof form != 'object' && form.nodeName != "FORM") return;
const idDisqualified = el => (el.name && !el.disabled && el.type != 'file' && el.type != 'reset' && el.type != 'submit' && el.type != 'button');
const isChecked = el => (el.type != 'checkbox' && el.type != 'radio') || el.checked;
return Array
.from(form.elements)
.filter(el => idDisqualified(el))
.filter(el => isChecked(el))
.reduce((result, el) => {
result[el.name] = el.value;
return result;
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment