Skip to content

Instantly share code, notes, and snippets.

@sampotts
Last active July 26, 2016 06:47
Show Gist options
  • Save sampotts/55cb29f1e98d0e374fb53f4739f154d9 to your computer and use it in GitHub Desktop.
Save sampotts/55cb29f1e98d0e374fb53f4739f154d9 to your computer and use it in GitHub Desktop.
(function($) {
'use strict';
var values = {},
settings = [];
// Grab all inputs
$(".generated-form :input").each(function() {
var $input = $(this),
name = $input.attr("name"),
value = $input.val();
// Ignore any with no name attribute, hidden fields (for checkboxes)
// or "null" values (which clear values server side)
if (!name || (name && name.endsWith("__hidden")) || value === "null") {
return true;
}
// Special treatment required for radio and checkbox arrays
if ($input.is(":radio") || $input.is(":checkbox")) {
if ($input.is(":checked")) {
if (name in values) {
values[name] += "," + value;
}
else {
values[name] = value;
}
}
}
else {
values[name] = value;
}
});
// Change shape to match style definition
$.each(values, function(key, value) {
settings.push({ "name": key, "value": value });
});
// Sort alphabetically based on name field
settings.sort(function(a, b){
var nameA = a.name.toLowerCase(),
nameB = b.name.toLowerCase();
// Sort string ascending
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// Default return value (no sorting)
return 0;
});
// Output to console in 4 space tab
window.console.log(JSON.stringify(settings, null, 4));
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment