You can use an array of objects:
press = [
{ desc: 'T shirt black',
price: 23
},
{ desc: 'Sweater black'
price: 34
},
{ desc: 'Hoodie shirt black'
price: 87
}
];
You can then access them with press[i].desc
and press[i].price
.
press.forEach(function(item) {
$('#item').append($('<option/>', {
value: item.price,
text: item.desc+' - $'+item.price
}));
});
Here's the full rewrite of your fiddle. I also got rid of eval()
, by putting all the different arrays into an object keyed off the category value.
$(document).ready(function () {
dropdowns = {
web: [
{desc: "1 Custom Web Page", price: 39},
{desc: "5 Custom Web Pages", price: 190},
{desc: "10 Custom Web Pages", price: 375},
{desc: "20 Custom Web Pages", price: 710}
],
art: [{desc: '300-word Article x 1', price: 7.00},
{desc: '300-word Article x 5', price: 32.00},
{desc: '400-word Article x 1', price: 8.00},
{desc: '400-word Article x 5', price: 37.00},
{desc: '500-word Article x 1', price: 9.00},
{desc: '500-word Article x 5', price: 40.00},
{desc: '700-word Article x 1', price: 12.00},
{desc: '700-word Article x 5', price: 57.00},
{desc: '1000-word Article x 1', price: 15.00},
{desc: '1000-word Article x 5', price: 70.00}],
blog: [{desc: '300-word Custom Blog x 1', price: 10},
{desc: '400-word Custom Blog x 1', price: 12},
{desc: '500-word Custom Blog x 1', price: 14},
{desc: '300-word Custom Blog x 5', price: 47},
{desc: '400-word Custom Blog x 5', price: 55},
{desc: '500-word Custom Blog x 5', price: 63}
],
press: [{desc: '300-word Custom Press Release', price: 27},
{desc: '400-word Custom Press Release', price: 37},
{desc: '500-word Custom Press Release', price: 47}
]
}
populateSelect();
$(function () {
$('#cat').change(populateSelect);
});
function populateSelect() {
var cat = $('#cat').val();
$('#item').empty();
dropdowns[cat].forEach(function(item) {
$('#item').append($('<option/>', {
value: item.price,
text: item.desc+' = $'+item.price
}));
});
}
});
DEMO