Skip to content

Instantly share code, notes, and snippets.

@JaymzZh
Forked from Onheiron/toJSON.js
Created March 4, 2014 15:21
Show Gist options
  • Save JaymzZh/9348391 to your computer and use it in GitHub Desktop.
Save JaymzZh/9348391 to your computer and use it in GitHub Desktop.
$.fn.toJSO = function () {
var obj = {},
$kids = $(this).children('[name]');
if (!$kids.length) {
return $(this).val();
}
$kids.each(function () {
var $el = $(this),
name = $el.attr('name');
if ($el.siblings("[name=" + name + "]").length) {
if (!/radio|checkbox/i.test($el.attr('type')) || $el.prop('checked')) {
obj[name] = obj[name] || [];
obj[name].push($el.toJSO());
}
} else {
obj[name] = $el.toJSO();
}
});
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment