How do I dynamically pass a variable to a URL to fetch data from an API endpoint?

All,

I have a website that fetches a list of publicly-traded companies from an API endpoint. I take that returned JSON data to then populate a dropdown element called, “mySelect”, with the company stock ticker symbols. I need to do two things:

  1. Pass the first company’s symbol that gets populated in the dropdown menu to a query parameter in a URL that will then fetch the price data for that company so that I can display it on a chart.

  2. Change the symbol that gets passed to that price data URL from the first company’s symbol that gets loaded when the page loads, to the symbol of the company that is selected from the dropdown menu.

I’m struggling to figure out how to do this in the same script. I’d previously had two different javascript files – one that grabs the first value on page load, and the second grabbed the selected company value. This seemed really duplicative. Not sure why I can’t wrap my head around how to pass these values dynamically to the second URL. Any guidance would be appreciated. I’m happy to provide additional information. Thanks!

Sample abridged code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// function to handle fetches:
function getCompanies(url) {
return fetch(url)
.then(response => {
if(!response.ok) {
throw new Error('Bad Network Request');
}
return response.json();
})
.catch(error => {
console.error('Fetch error:', error);
throw error;
});
}
// use the getCompanies function to fetch the list of companies from an API endpoint and populate a dropdown menu on the webpage:
getCompanies('/api/company_list')
.then(function fetchData(data) {
let option = '';
for (let i = 0; i < data.length; i++) {
option += '<option value="' + data[i].symbol + '">' + data[i].stock_rank + ' - ' + data[i].company_name + ' (' + data[i].symbol + ')' + '</option>';
}
$('#mySelect').append(option);
})
.catch(error => {
console.error('Error:', error);
});
// use the getCompanies function to fetch the pricing data for the symbol selected in the dropdown menu:
getScorecard(`/api/prices?symbol=${symbol}`)
.then(function chartData(data) {
//do something with the data...
})
</code>
<code>// function to handle fetches: function getCompanies(url) { return fetch(url) .then(response => { if(!response.ok) { throw new Error('Bad Network Request'); } return response.json(); }) .catch(error => { console.error('Fetch error:', error); throw error; }); } // use the getCompanies function to fetch the list of companies from an API endpoint and populate a dropdown menu on the webpage: getCompanies('/api/company_list') .then(function fetchData(data) { let option = ''; for (let i = 0; i < data.length; i++) { option += '<option value="' + data[i].symbol + '">' + data[i].stock_rank + ' - ' + data[i].company_name + ' (' + data[i].symbol + ')' + '</option>'; } $('#mySelect').append(option); }) .catch(error => { console.error('Error:', error); }); // use the getCompanies function to fetch the pricing data for the symbol selected in the dropdown menu: getScorecard(`/api/prices?symbol=${symbol}`) .then(function chartData(data) { //do something with the data... }) </code>
// function to handle fetches:
function getCompanies(url) {
    return fetch(url)
        .then(response => {
            if(!response.ok) {
                throw new Error('Bad Network Request');
            }
            return response.json();
        })
        .catch(error => {
            console.error('Fetch error:', error);
            throw error;
        });
}

// use the getCompanies function to fetch the list of companies from an API endpoint and populate a dropdown menu on the webpage:
getCompanies('/api/company_list')
    .then(function fetchData(data) {
        let option = '';            
        for (let i = 0; i < data.length; i++) {
            option += '<option value="' + data[i].symbol + '">' + data[i].stock_rank + ' - ' + data[i].company_name + '  (' + data[i].symbol + ')' + '</option>';
        }
                    
        $('#mySelect').append(option);
    })
    .catch(error => {
        console.error('Error:', error);
    });

// use the getCompanies function to fetch the pricing data for the symbol selected in the dropdown menu:
getScorecard(`/api/prices?symbol=${symbol}`)
    .then(function chartData(data) {
        //do something with the data...
    })


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>document.getElementById("mySelect").onchange = function () {getSymbol()};
// store the selected company symbol as the variable "myVal" (need to pass this to the stock price URL)
function getSymbol() {
var myVal = $("#mySelect").val();
return myVal;
}
</code>
<code>document.getElementById("mySelect").onchange = function () {getSymbol()}; // store the selected company symbol as the variable "myVal" (need to pass this to the stock price URL) function getSymbol() { var myVal = $("#mySelect").val(); return myVal; } </code>
document.getElementById("mySelect").onchange = function () {getSymbol()};

// store the selected company symbol as the variable "myVal" (need to pass this to the stock price URL)
function getSymbol() {
    var myVal = $("#mySelect").val();
    return myVal;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật