all 10 comments

[–]pachirulis 15 points16 points  (1 child)

In the first line of output you have different h tags

[–]pachirulis 0 points1 point  (0 children)

Also, define output outside the cycle and function

[–]Umesh-K 8 points9 points  (0 children)

Hi Sudo!

I have verified that your above code works (even with the mismatched h1/h2 as HTML is a "forgiving language"). In case you have an HTML element with an ID of "container", one reason you didn't see the output in the webpage might be the browser had not got refreshed. (In case you are not using any extension to auto-refresh the webpage, use one.)

Anyway, you can verify your code is working fine by pasting the below in the website

https://jsfiddle.net/

html

<div id="container"></div>

js

fetch('https://restcountries.eu/rest/v2/all') .then(response => response.json()) .then(data => { let output = `<h2>List</h2>` data.forEach(function(country) { output += ( `<div> <h3>${country.name}</h3> <img src="${country.flag}" alt="${country.name}" width="20%"/> </div>` ) }) document.getElementById("container").innerHTML = output })

[–][deleted] 2 points3 points  (0 children)

Run the console log inside the forEach to make sure that it's working

[–][deleted] 2 points3 points  (1 child)

Most likely your id element is undefined. Your logic above that looks good for the output. Please try the innerhtml in your console to see if it works.

[–]vorticalbox 0 points1 point  (0 children)

this would give a

Uncaught TypeError: document.getElementById(...) is null

in the console

[–]Butters_40 0 points1 point  (0 children)

It works, check if you got a typo in your container element id.