Skip to content

objectifyForm

Mike Byrne edited this page Jan 25, 2022 · 4 revisions

description

Generates a JS Object out of a form. Note: does not add items from input[type=file] or input[disabled].

You may also consider using FormData noting IE11 and Safari 10 can only make FormData and not interrogate or update the data.

requires

  • nothing

parameters

  • form - required - form node

returns

  • JS Object compiled from the form

example usage:

<form id='form'>
  <label for='name'>Name</label>
  <input type='text' id='name' name='name' value='Mike'>
  <label for='description'>Description</label>
  <textarea id='description' name='description'>Interface Engineer</textarea>
  <label><input type='radio' name='role' value='FE' checked> FE</label>
  <label><input type='radio' name='role' value='BE'> BE</label>
  <label><input type='checkbox' name='staff' value='staff' checked> Staff</label>
</form>
var form = document.getElementsById('form');
var form_data = objectifyForm(form);
// Object {name: 'Mike', description: 'Interface Engineer', role: 'FE', staff: 'staff'}