Last active
December 21, 2015 01:39
-
-
Save simonbowen/6229731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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