Last active
August 29, 2015 14:20
-
-
Save jdcauley/ded68dbefe8b093b2af1 to your computer and use it in GitHub Desktop.
Putting form Data into an Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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