Skip to content

Instantly share code, notes, and snippets.

@wesen
Created September 6, 2011 17:35
Show Gist options
  • Select an option

  • Save wesen/1198341 to your computer and use it in GitHub Desktop.

Select an option

Save wesen/1198341 to your computer and use it in GitHub Desktop.
get form data as json
/**
* Extract the value of all form fields specified in fields.
**/
$.fn.getFormData = function (fields) {
var res = {};
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
res[field] = $(this).find("[name=" + field + "]").val();
}
return res;
};
/**
* Returns all the input fields of the form.
**/
$.fn.getAllFormData = function () {
var res = {};
$(this).find("input, textarea, select, checkbox").each(function (idx, elt) {
elt = $(elt);
res[elt.attr("name")] = elt.val();
});
return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment