I'm supposed to create a loops to output several images with styling. I'm having trouble with the correct syntax and path to the images.
I have tried doing this with relative path (see below JavaScript file) but I still can't see the output of the images on the html.
This is my JSON file (photos.json)
const content = `
{
"id":"3a5c5da5-5a12-4d2b-b0dd-abc28eaf810b",
"title":"British Museum",
"description":"The library in the British Museum in London. The British Museum Reading Room, situated in the centre of the Great Court of the British Museum, used to be the main reading room of the British Library. In 1997, this function moved to the new British Library building at St Pancras, London, but the Reading Room remains in its original form at the British Museum.",
"location":{
"iso":"GB",
"country":"United Kingdom",
"city":"London",
"cityCode":2643743,
"continent":"EU",
"latitude":51.519148,
"longitude":-0.126826
},
"filename":"5855729828.jpg",
"colors":[
{
"hex":"#a9b490",
"name":"Norway"
},
{
"hex":"#bab984",
"name":"Pine Glade"
},
{
"hex":"#71735c",
"name":"Finch"
},
{
"hex":"#332625",
"name":"Wood Bark"
},
{
"hex":"#b99a5d",
"name":"Barley Corn"
}
]
}
...
This is my HTML file. And I'm not supposed to add anything onto this file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lab 8</title>
<link href="https://fonts.googleapis.com/css?family=Oswald:300,500" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/lab08.css" />
</head>
<body>
<main id="test4">
<header>
<h1>Test Your Knowledge #4</h1>
</header>
<section>
<script src="js/photos.json"></script>
<script src="js/lab08-test04.js"></script>
</section>
</main>
</body>
</html>
And this is my Javascript file so far... The html is in the main folder, the images are in another folder named "images", so I tried to use images/img.jpg path to get the image on JavaScript but failed, please help.
const photos = JSON.parse(content);
document.write('<img src="' + photos[0].filename + '">');
That does not work but this code does, but i dont wanna do this way cus i have to use loops:
document.write('<img src="images/5855729828.jpg">');
Thanks!