Created
December 12, 2011 15:52
-
-
Save ronanguilloux/1467980 to your computer and use it in GitHub Desktop.
Extract form values as an object (= a JSON .serialize() alternative)
This file contains hidden or 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
// source : http://goo.gl/bJmRv, augmented with a little checkboxes handler | |
$.fn.extractObject = function() { | |
var accum = {}; | |
function add(accum, namev, value) { | |
if (namev.length == 1) | |
accum[namev[0]] = value; | |
else { | |
if (accum[namev[0]] == null) | |
accum[namev[0]] = {}; | |
add(accum[namev[0]], namev.slice(1), value); | |
} | |
}; | |
this.find('input, textarea, select').each(function() { | |
// checkboxes' "checked" attribute handling : | |
if( $(this).attr('checked') && ('checked' == $(this).attr('checked'))) { | |
$(this).val(true); | |
} | |
add(accum, $(this).attr('name').split('.'), $(this).val()); | |
}); | |
return accum; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment