Hi,
This is probably a common question, but I do not know how to ask. It is about fetching data from an api and show on a web page. The results seems to be OK in an alert message, but not presented as a list in the innerHTML.
Any clue what I am doing wrong?
TIA,
[code]
let Text = document.getElementById(“text”)
let URL = “http://127.0.0.1:8081/co”;
function getPost() {
fetch(URL)
.then(res => res.json())
.then((data) => {
data.forEach(function(co) {list += `<li>${co.company}</li>`})
alert(list) <— the list is correct in this variable
Text.innerHTML=(list) <— THE PROBLEM – will NOT show up in the page
Text.innerHTML=(“list”)<— This will show up
})
}
Edited by Sempervivum: Inserted code tags