Kgiannakakis' suggestion for moving around the map was a good starting point, although I do not feel that it qualifies as an answer to my original question. After several hours of giddiness, I decided for this, which allows you to serialize elements based on a user attribute (I did not want to agree to use the "name" attribute in my form elements, which requires jQuery). I also started using the JSON library from json.org to create the object that I am creating. The plugin serializeToJSON function is what I was looking for as an answer to my question, the rest is just exta.
Note This is for the client, so CustomXXX names and attributes have been replaced with what they are actually
jQuery.fn.extend({ serializeCustomPropertyArray: function() { return this.map(function() { return this.elements ? jQuery.makeArray(this.elements) : this; }).filter(function() { return jQuery(this).attr('CustomAttribute') && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password|search/i.test(this.type)); }).map(function(i, elem) { var val = jQuery(this).val(); return val == null ? null : jQuery.isArray(val) ? jQuery.map(val, function(val, i) { return { name: jQuery(elem).attr('CustomAttribute'), value: val }; }) : { name: jQuery(elem).attr('CustomAttribute'), value: val }; }).get(); }, serializeToJSON: function() { var objectMap = this.serializeCustomPropertyArray(); var objectJson = new Object; jQuery.each(objectMap, function() { objectJson[this.name] = (this.value !== null) ? this.value : 'null'; }); return JSON.stringify(objectJson); } });
This can be called the following:
$('#fields').find(':input[CustomGroup="Months"]').serializeToJSON();
Assuming your document looks something like this:
<div id="fields"> <input type="checkbox" CustomGroup="Months" CustomAttribute="January" />January<br /> <input type="checkbox" CustomGroup="Months" CustomAttribute="February" />February<br /> ... </div>
The generated JSON looks like this:
{ January: 'on', February: 'on', ... }
source share