I'm just trying to generate a table using info from a JSON file but it is not working - I am new to HTML so I can't see what I am doing wrong. The JSON file called food.json is just like:
[
{
"Type": "Snack",
"Flavour": "Salted",
"Brand": "Walkers"
},
{
"Type": "Dessert",
"Flavour": "Chocolate",
"Brand": "Alpro",
}
]
I have a javascript file called test.js with the following:
$(document).ready(function () {
$.getJSON("food.json",
function (data) {
var food = '';
$.each(data, function (key, value) {
food += '<tr>';
food += '<td>' +
value.Type + '</td>';
food += '<td>' +
value.Flavour + '</td>';
food += '<td>' +
value.Brand + '</td>';
food += += '</tr>';
});
$('#table').append(food);
});
});
And below is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="jquery-3.5.1.js"></script>
<script src="test.js"></script>
<title>Fave Food</title>
</head>
<body>
<h1>My Fave Food</h1>
<form>
<button onclick="getJSON()">Click for My Fave Food!</button>
</form>
<table id="table">
<thead>
<tr>
<th>Type</th>
<th>Flavour</th>
<th>Brand</th>
</tr>
</thead>
</table>
</body>
</html>