I'm still learning javascript and need little help.
Project is about display data from API in dropdown list, what I already did, and its working.
API LINK: http://api.football-data.org/v1/competitions
I want someone to help me with code, when you click on some value inside box, to display data for that ID
Example:
Dropdownlist showing data by:
caption
and every "caption" has unique ID, Example:
id: 444, caption: "Campeonato Brasileiro da Série A",
When I click on that option to show me TEAMS from that Football League.
Teams are in separate link:
I do not know how from here?!
let dropdown = document.getElementById('locality-dropdown');
dropdown.length = 0;
let defaultOption = document.createElement('option');
defaultOption.text = 'Choose...';
dropdown.add(defaultOption);
dropdown.selectedIndex = 0;
const url = 'http://api.football-data.org/v1/competitions';
fetch(url)
.then(
function(response) {
if (response.status !== 200) {
console.warn('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement('option');
option.text = data[i].caption;
option.value = data[i].id;
dropdown.add(option);
}
});
}
)
.catch(function(err) {
console.error('Fetch Error -', err);
});
<!DOCTYPE html>
<html>
<head>
<title>Football-Data API</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="display-4 mb-4">www.football-data.org</h1>
<div class="d-flex">
<div class="form-group">
<label for="sel1">Select competitions:</label>
<select class="form-control" id="locality-dropdown" onchange=(myFunction)>
<option></option>
</select>
</div>
</div>
<hr>
<div id="output"></div>
</div>
</body>
</html>