MLB - XHR
/* mlb api; sportId = 1 is major league */
let urlTeams = "https://statsapi.mlb.com/api/v1/teams?sportId=1",
xhr = new XMLHttpRequest();
document.addEventListener("DOMContentLoaded", function () {
console.log("dom ready");
xhr.responseType = "json";
xhr.open("GET", urlTeams);
xhr.addEventListener("load", buildList);
xhr.send();
});
function buildList() {
let teams = xhr.response.teams;
teams.sort((a, b) => (a.name > b.name ? 1 : -1));
let teamlist = document.createElement("ul");
for (const team of teams) {
console.log(team);
console.log(team.name);
let myli = document.createElement("li");
let myanchor = document.createElement("a");
let mytext = document.createTextNode(team.name);
let teamUrlPath = team.teamName.toLowerCase().replace(" ", "");
let myteamhref = "https://mlb.com/" + teamUrlPath;
myanchor.setAttribute("href", myteamhref);
teamlist.appendChild(myli).appendChild(myanchor).appendChild(mytext);
}
document.getElementById("teamscontainer").appendChild(teamlist);
}