forked from willeeklund/JavaScriptForAnalysts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.serializeObject.js
More file actions
27 lines (22 loc) · 809 Bytes
/
Copy pathjquery.serializeObject.js
File metadata and controls
27 lines (22 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$.fn.serializeObject = function () {
var
result = Object.create(null),
mapper = function (element) {
element.name = $.camelCase(element.name);
return element;
},
extend = function (i, element) {
var node = result[element.name];
// If node with same name exists already, need to convert it to an array as it
// is a multi-value field (i.e., checkboxes)
if ('undefined' !== typeof node && node !== null) {
result[element.name] = node.push ? node.push(element.value) : [node, element.value];
} else {
result[element.name] = element.value;
}
};
// For each serialzable element, convert element names to camelCasing and
// extend each of them to a JSON object
$.each($.map(this.serializeArray(), mapper), extend);
return result;
};