Skip to content

Instantly share code, notes, and snippets.

@aaitken
Created July 6, 2011 03:31
Show Gist options
  • Save aaitken/1066500 to your computer and use it in GitHub Desktop.
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
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