Skip to content

Instantly share code, notes, and snippets.

@jgimbel
Last active February 29, 2016 19:24
Show Gist options
  • Save jgimbel/9eade21249d800c8eb1f to your computer and use it in GitHub Desktop.
Save jgimbel/9eade21249d800c8eb1f to your computer and use it in GitHub Desktop.
form-to-object.js
const formToObj = ({ elements }) => elements.reduce((prev, { name, value }) => prev[name] = value, {})
var myForm = document.querySelector('form')
myForm.addEventListener('submit', function (e) {
e.preventDefault()
fetch('/', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
student: formToObj(myForm)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment