Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Last active August 29, 2015 14:20
Show Gist options
  • Save jdcauley/ded68dbefe8b093b2af1 to your computer and use it in GitHub Desktop.
Save jdcauley/ded68dbefe8b093b2af1 to your computer and use it in GitHub Desktop.
Putting form Data into an Object
function getParams(form){
var params = {};
params.action = form.action;
params.formId = form.id;
var inputs = form.getElementsByTagName('input');
for(var i = 0, x = inputs.length; i < x; i++){
var name = inputs[i].getAttribute('name');
var val = inputs[i].value;
if(name){
params[name] = val;
}
}
return params;
}
$('#form-id').on('submit', function(e){
// validation code here
e.preventDefault();
var data = getParams(this);
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment