var articles; axios.get("136.145.231.32:8080") .then(response => { articles = response.data; for (let i = 0; i < articles.length; i++) { let element = articles[i]; axios.get(`136.145.231.32:8080/${element}`) .then(response => { let articlesHeading = document.querySelector("#articles-heading").parentElement; let articleButton = document.createElement("button"); articleButton.textContent = response.data; articleButton.classList.add("m-5", "article-content"); articleButton.setAttribute("data-toggle", "modal"); articleButton.setAttribute("data-target", `#articleModal${i}`); let modal = document.createElement("div"); modal.classList.add("modal", "fade"); modal.setAttribute("id", `articleModal${i}`); modal.setAttribute("tabindex", "-1"); modal.setAttribute("aria-labelledby", `articleModalLabel${i}`); modal.setAttribute("aria-hidden", "true"); modal.innerHTML = ` `; document.body.appendChild(modal); articlesHeading.appendChild(articleButton); }) .catch(error => console.error(error)); } }) .catch(error => console.error(error));