I am trying to access data from an API, add it to a dropdown menu, and then have a picture (URL is in the API) added when the appropriate name from the dropdown is clicked.
I have successfully created the dropdown menu in populateDropdown()
, but I want to be able to access the dropdown options in completely different functions so I can create a new function. I can't figure out where I should put my return statement.
I would like to avoid Jquery if I can.
const baseURL = 'https://redacted.com'
const rolesURL = "roles"
const usersURL = "users"
function getHandlers(response) {
fetch(baseURL + rolesURL)
.then(response => response.json())
.then(populateDropdown)
}
function populateDropdown(response) {
let drop = document.getElementById("drop")
response.forEach(function (labels) {
let option = document.createElement("option")
option.innerHTML = labels.label
option.setAttribute("id", labels.id)
drop.appendChild(option)
})
}
I have tried searching, but cannot find a solution that doesn't use Jquery, or is from this decade.
This is my first StackOverflow question, so I apologize if there was something I didn't do.
For whoever marked my question as a duplicate, could you please provide me with the link to the question that has eluded me after an hour of searching?