Skip to content

Instantly share code, notes, and snippets.

@Mamaduka
Last active August 25, 2019 13:02
Show Gist options
  • Save Mamaduka/164bc6d17281aa0656dc208376ec6c69 to your computer and use it in GitHub Desktop.
Save Mamaduka/164bc6d17281aa0656dc208376ec6c69 to your computer and use it in GitHub Desktop.
Get form data as name => value pair in ES6.
const form = document.getElementById( 'acme-form' );
// Checkboxe, select and radios are handled differently
const dataArray = Array
.from( form.elements )
.filter( input => ( input.type === 'text' || input.type === 'textarea' ) )
.map( input => {
return { [input.name] : input.value }
} );
// Convert collected data array into object.
const data = Object.assing( ...dataArray );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment