Created
July 6, 2011 03:31
-
-
Save aaitken/1066500 to your computer and use it in GitHub Desktop.
basic amd module for converting form inputs and values to a JSON string
This file contains 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
define([],function(){ | |
var formToJson=function(e){ //e = submit event | |
var srcObj={}, //object 2b stringified | |
forEach=Array.prototype.forEach; //2b used on form.elements, which is an array-like html node collection | |
e.preventDefault(); //prevent default form submit | |
forEach.call(e.target.elements,function(item){ //function's param via JS spec on forEach (which also includes two others) | |
srcObj[item.name]=item.value; | |
}); | |
return JSON.stringify(srcObj); | |
}; | |
return formToJson; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/aaitken/modlets/blob/master/scripts/modules/formToJson.js