Skip to content

Instantly share code, notes, and snippets.

@simonbowen
Last active December 21, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbowen/6229731 to your computer and use it in GitHub Desktop.
Save simonbowen/6229731 to your computer and use it in GitHub Desktop.
$(function(){
var products = [
{name: 'iPad', description: 'iPad', price: 300, applecare: 50, insurance: 60},
{name: 'ipad2', description: 'ipad2', price: 350, applecare: 55, insurance: 65}
];
// Then build the table from the Javascript array/object
var table = $('table');
for(x=0; x<products.length; x++){
var tr = $('<tr>');
tr.html('<td>' + products[x].name + '</td><td>' + products[x].price + '</td>');
table.append(tr);
}
// Build your select box
var select = $('select');
for(x=0; x<products.length; x++)
{
var option = $('<option>').val(products[x].name).html(products[x].name);
select.append(option);
}
// This will run on select box change
select.change(function(){
var selected_option = $(this).find('option:selected');
var selected_index = selected_option.index();
var product = products[selected_index];
console.log(product.name, product.price, ...);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment