I have a JSON string from which I want to create multiple selectboxes. The problem however is that one value is somewhere deeper hidden in the JSON file. Further the json file is created dynamically so everything should work dynamically.
So my question is how to create the selectboxes and how do I get the value which lays deeper in the JSON file into the same select box?
JSON
{
"product": {
options": {
"58395": {
"id": 58395,
"title": "Size",
"values": {
"220544": {
"id": 220544,
"title": "36",
"active": true
},
"220545": {
"id": 220545,
"title": "37",
"active": false
},
"220546": {
"id": 220546,
"title": "38",
"active": false
},
"220547": {
"id": 220547,
"title": "39",
"active": false
}
}
},
"61295": {
"id": 61295,
"title": "Color",
"values": {
"231911": {
"id": 231911,
"title": "Zwart",
"active": true
},
"231912": {
"id": 231912,
"title": "Bruin",
"active": false
},
"231913": {
"id": 231913,
"title": "Rood",
"active": false
}
}
},
"61296": {
"id": 61296,
"title": "Fabric",
"values": {
"231914": {
"id": 231914,
"title": "1",
"active": true
},
"231915": {
"id": 231915,
"title": "2",
"active": false
},
"231916": {
"id": 231916,
"title": "3",
"active": false
}
}
}
}, // etc....
So what I try to do is create 3 selectboxes -> Size, color and fabric. Inside these selectboxes have to come the value title. So you get something like:
<option value=' + option.id + '>' + value.title + '</option>
So what I tried was this, which obviously doesn't work:
$.getJSON(url, function (data){
var $selectOptions = $('<select />');
$.each(data.product.options, function (index, option){
var ids = "";
$.each(options.values, function (i, v) { ids += " " + v.id; });
$selectOptions.append('<option value=' + option.id + '>' +option.value.title + ids + '</option>');
}); //etcetc...
This generates just one selectbox with with all option names inside it. Instead of the value titles.
Can anybody help me with this. I really can't figure this one out.
Thanks!