Instantly share code, notes, and snippets.

Embed
What would you like to do?
Runtime injection of optios/optgroups to jQM select.
var deliveryJSON = {
"optGroups": [{
"label": "FedEx",
"options": [{
"value": "firstOvernight",
"label": "First Overnight"
}, {
"value": "expressSaver",
"label": "Express Saver"
}, {
"value": "ground",
"label": "Ground"
}]
}, {
"label": "UPS",
"options": [{
"value": "firstOvernight",
"label": "First Overnight"
}, {
"value": "expressSaver",
"label": "Express Saver"
}, {
"value": "ground",
"label": "Ground"
}]
}, {
"label": "US Mail",
"options": [{
"value": "standard",
"label": "Standard: 7 day"
}, {
"value": "rush",
"label": "Rush: 3 days"
}, {
"value": "express",
"label": "Express: next day"
}, {
"value": "overnight",
"label": "Overnight"
}]
}]
};
var shippingJSON = {
"methods": [{
"value": "standard",
"label": "Standard: 7 day"
}, {
"value": "rush",
"label": "Standard: 7 day"
}, {
"value": "express",
"label": "Express: next day"
}, {
"value": "overnight",
"label": "Overnight"
}]
};
function fillShipping() {
var _options = [],i;
for (i = 0; i < shippingJSON.optGroups.length; i++) {
_options.push('<option value="' + shippingJSON.methods[i].value + '">' + shippingJSON.methods[i].label + '</option>');
}
$('#shippingMethods').append(_options.join("")).selectmenu('refresh');
}
function fillDelivery() {
var _options = [],i, j;
for (i = 0; i < deliveryJSON.optGroups.length; i++) {
_options.push('<optgroup label="' + deliveryJSON.optGroups[i].label + '">');
for (j = 0; j < deliveryJSON.optGroups[i].options.length; j++) {
_options.push('<option value="' + deliveryJSON.optGroups[i].options[j].value + '">' + deliveryJSON.optGroups[i].options[j].label + '</option>');
}
_options.push('</optgroup>');
}
$('#deliveryMethods').append(_options.join("")).selectmenu('refresh');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment