This web page has three cards with information above and an image below. I am using JavaScript and HTML to get the information that goes to my web page. My JavaScript gets the information it needs from the following online JSON file. The file doesn’t contain alt text for the image. when I run the Wave evaluation tool, it says I need alt text for my images. The images are provided through the JSON file. How can I create alt text for it? Do I do it in the HTML or in the JavaScript? How would I do that?
JavaScript
x
97
97
1
Array(3)
2
0:
3
averageRainfall: 14.2
4
currentPopulation: 501
5
motto: "This is Fish Heaven."
6
name: "Fish Haven"
7
photo: "fishhaven.jpg"
8
yearFounded: 1864__proto__: Object
9
10
1: averageRainfall: 16.65
11
currentPopulation: 5204
12
motto: "Home of Napoleon Dynamite."
13
name: "Preston"
14
photo: "preston.jpg"
15
yearFounded: 1866
16
__proto__: Object
17
18
2:
19
averageRainfall: 15.75
20
currentPopulation: 2985
21
motto: "Historic Oregon Trail Oasis. The Soda is on Us.
22
"name: "Soda Springs"
23
photo: "sodasprings.jpg"
24
yearFounded: 1858
25
__proto__: Object
26
27
28
29
MY JAVASCRIPT file js/home.js
30
//set JSON source
31
const requestURL = 'https://byui-cit230.github.io/weather/data/towndata.json';
32
33
//fetch data
34
fetch(requestURL)
35
.then(function (response) {
36
return response.json();
37
})
38
39
.then(function (jsonObject){
40
const towns = jsonObject['towns'];
41
42
//create town input
43
const fishhaven = towns.filter(x => x.name === "Fish Haven");
44
const preston = towns.filter(x => x.name === "Preston");
45
const sodasprings = towns.filter(x => x.name === "Soda Springs");
46
47
const sort = [];
48
sort.push(fishhaven, preston, sodasprings)
49
console.log(sort);
50
51
// let eachTown = towns.sort('sort');
52
sort.forEach(town => {
53
let card = document.createElement('div');
54
let info = document.createElement('section');
55
let name = document.createElement('h2');
56
let motto = document.createElement('h3');
57
let year = document.createElement('p');
58
let pop = document.createElement('p');
59
let rain = document.createElement('p');
60
let photo = document.createElement('img');
61
62
//use template literals
63
name.textContent = `${town.name}`;
64
motto.textContent = `${town.motto}`;
65
year.textContent = `Year Founded: ${town.yearFounded}`;
66
pop.textContent = `Population: ${town.currentPopulation}`;
67
rain.textContent = `Annual Rainfall: ${town.averageRainfall}`;
68
photo.src = `images/${town.photo}`;
69
70
card.append(info);
71
info.append(name);
72
info.append(motto);
73
info.append(year);
74
info.append(pop);
75
info.append(rain);
76
card.append(photo);
77
document.querySelector(".towns").appendChild(card);
78
});
79
})
80
81
82
83
HTML only consists of:
84
<!DOCTYPE html>
85
<html lang="en-us">
86
<title>Whether to Weather Home</title>
87
<body>
88
<main class="homepage">
89
<div class="hero-div">
90
<img src="images/prestonhero.jpg" alt="image of mountains near Preston, Idaho.">
91
</div>
92
</main>
93
<footer></footer>
94
<script src="js/home.js"></script>
95
</body>
96
</html>
97
Advertisement
Answer
Just add an alt
attribute to photo
JavaScript
1
6
1
let photo = document.createElement('img');
2
photo.src = `images/${town.photo}`;
3
4
// What you need is the line below
5
photo.alt = `${town.name}`;
6