There are JSON files embedded in the page, like so:
<script type="text/javascript" language="javascript" src="json/divaniModerni.json"></script>
<script type="text/javascript" language="javascript" src="json/divaniClassici.json"></script>
All of them have the same structure with different elements:
var divaniModerni = {
"modelli": [
{
"nome": "California",
"num": "5",
},
{
"nome": "Terra",
"num": "6",
},
{
"nome": "Laura",
"num": "7",
},
{
"nome": "Nonstop",
"num": "11",
},
{
"nome": "Venere",
"num": "8",
},
{
"nome": "Comfort",
"num": "5",
},
{
"nome": "Infinity",
"num": "8",
},
]
}
I can parse the file like this:
$(divaniModerni.modelli).each(function(index, element){ (...) }
Is it possible to dynamically change the file to parse by passing the name to a function like this?
function show(category)
{
$(category.modelli).each(function(index, element){ (...) }
}
show(divaniModerni);
I tried using:
$(window[category].modelli).each(function(index, element){ (...) }
but it is not working...
EDIT:
Within the each loop, I am dynamically creating rows in a table based on the selected JSON elements:
$(divaniModerni.modelli).each(function(index, element){
if (i == 1)
row += "<tr>";
row += "<td><figure><a class='thumbnails' rel='prettyPhoto[gallery" + i + "]' href='images/divani/" + element.nome + ".jpg'><img src='images/thumbnails/divani/" + element.nome + ".jpg' alt='" + element.nome + "'></a><div class='description'>" + element.nome;
if (element.num > 0)
{
for (j = 2; j <= element.num; j++)
{
row += "<a style='display:none;' class='thumbnails' rel='prettyPhoto[gallery" + i + "]' href='images/divani/" + element.nome + j + ".jpg'><img src='images/anteprima/divani/" + element.nome + j + ".jpg' alt='" + element.nome + "'></a>";
}
}
row += "</div></figure></td>";
if (i == category.modelli.length)
{
row += "</tr>";
$('#show').append(row);
}
else if (i % 4 == 0)
{
row += "</tr>";
$('#show').append(row);
row = "<tr>";
}
i++
})