I'm trying to output the results of a JSON file in a table. I'm able to see the data and loop thru each data but I'm unable to display that in the table.
When troubleshooting I get, "script.js:25 Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerHTML')
at outData (script.js:25:34)
at script.js:6:17"
I've looked online and I'm unable to find the solution to this. Any help is greatly appreciated.
HTML
<body>
<div class="stats">
<table>
<thead>
<tr>
<th>POS</th>
<th>Name</th>
<th>APP</th>
<th>Goals</th>
<th>Assists</th>
<th>Clean Sheets</th>
</tr>
</thead>
<tbody id="stats-output">
</tbody>
</table>
</div>
<script src="script.js"></script>
</body>
JS
fetch("data.json")
.then(Response => Response.json())
.then(data => {
outData(data.Sheet1);
})
function outData(val){
console.log(val);
let output = document.getElementById("#stats-output");
let html = '';
val.forEach((player, index) =>{
console.log(player);
html += `
<td>${player.Position}</td>
<td>${player.Name}</td>
<td>${player.Apperiences}</td>
<td>${player.Assists}</td>
<td>${player.Clean_Sheets}</td>
`;
})
output.innerHTML = html;
}
data example
{
"Position": "ST",
"Name": "Ronaldo",
"Apperiences": "50",
"Assists": "11",
"Clean_Sheets": "10"
},
outputelement variable is null. This is probably because you have a number sign (#) in the idlet output = document.getElementById("#stats-output");. Take that out and it should work. If you are usingdocument.querySelector()then you would include the number sign.