const getSelectValue = (callback) => { const options = getElement("#city-select"); options.addEventListener("change", function (e) { const value = e.target.value; // pass the value into the callback function we can use later callback(value); // this value needs to be passed into the function where we are using the getSelectValue function ie getSelectValue(value) }); }; // app function const init = () => { // populate the DOM with the cities data on load const selectDOM = getElement("#city-select"); populateDOM(cities, selectDOM); let selectedCity = ""; // this gets updated with the function below // need to run the callback here // get the value and assign it to a variable outside the function getSelectValue((value) => { selectedCity = value; // Assign the value to selectedCity // // Log the selected city if (selectedCity !== "") { const container = getElement(".container h3"); container.textContent = selectedCity; } }); }; window.addEventListener("DOMContentLoaded", init);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter