I cannot seem to get the categories(planets,dwarfplanets and other) inside the dropdown menu. I know I should use data.something but idk what, any help?
HTML
JavaScript
x
2
1
<select id="categories"></select>
2
The link to my json api
[https://howest-gp-wfa.github.io/st-2021-1-S2-ee-solar-system-Jonas-Bundervoet/api/data.json][1]
For my javascript I have this
JavaScript
1
39
39
1
"use strict"
2
3
window.addEventListener("load", Init);
4
5
var categories;
6
7
function Init()
8
{
9
categories = document.getElementById("categories");
10
11
FetchData();
12
}
13
14
15
function FetchData(){
16
fetch("https://howest-gp-wfa.github.io/st-2021-1-S2-ee-solar-system-Jonas-Bundervoet/api/data.json")
17
.then(
18
function(response) {
19
if (response.status !== 200) {
20
console.warn('Looks like there was a problem. Status Code: ' +
21
response.status);
22
return;
23
}
24
response.json().then(function(data) {
25
let option;
26
27
for (let i = 0; i < data.length; i++) {
28
option = document.createElement('option');
29
option.text = data[i].name;
30
categories.add(option);
31
}
32
});
33
}
34
)
35
.catch(function(err) {
36
console.error('Fetch Error -', err);
37
});
38
}
39
Advertisement
Answer
You are pretty close. To get all the different types out of your data object you could use Object.keys then replace data
in your loop with data[type]
.
Instead of add
you need to use appendChild to add elements into another DOM node.
JavaScript
1
42
42
1
"use strict"
2
3
window.addEventListener("load", Init);
4
5
var categories;
6
7
function Init()
8
{
9
categories = document.getElementById("categories");
10
11
FetchData();
12
}
13
14
15
function FetchData(){
16
fetch("https://howest-gp-wfa.github.io/st-2021-1-S2-ee-solar-system-Jonas-Bundervoet/api/data.json")
17
.then(
18
function(response) {
19
if (response.status !== 200) {
20
console.warn('Looks like there was a problem. Status Code: ' +
21
response.status);
22
return;
23
}
24
response.json().then(function(data) {
25
let option;
26
27
const types = Object.keys(data);
28
29
for(const type of types) {
30
for (let i = 0; i < data[type].length; i++) {
31
option = document.createElement('option');
32
option.text = data[type][i].name;
33
categories.appendChild(option);
34
}
35
}
36
});
37
}
38
)
39
.catch(function(err) {
40
console.error('Fetch Error -', err);
41
});
42
}
JavaScript
1
1
1
<select id="categories"></select>
EDIT: Categories instead of planet names
JavaScript
1
40
40
1
"use strict"
2
3
window.addEventListener("load", Init);
4
5
var categories;
6
7
function Init()
8
{
9
categories = document.getElementById("categories");
10
11
FetchData();
12
}
13
14
15
function FetchData(){
16
fetch("https://howest-gp-wfa.github.io/st-2021-1-S2-ee-solar-system-Jonas-Bundervoet/api/data.json")
17
.then(
18
function(response) {
19
if (response.status !== 200) {
20
console.warn('Looks like there was a problem. Status Code: ' +
21
response.status);
22
return;
23
}
24
response.json().then(function(data) {
25
let option;
26
27
const types = Object.keys(data);
28
29
for(const type of types) {
30
option = document.createElement('option');
31
option.text = type;
32
categories.appendChild(option);
33
}
34
});
35
}
36
)
37
.catch(function(err) {
38
console.error('Fetch Error -', err);
39
});
40
}
JavaScript
1
1
1
<select id="categories"></select>