-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateDropdown.js
More file actions
33 lines (31 loc) · 1.15 KB
/
populateDropdown.js
File metadata and controls
33 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.addEventListener('DOMContentLoaded', () => {
// Fetch and populate dropdown menus
fetchDropdownData('sizeDropdown', '/api/size');
fetchDropdownData('typeDropdown', '/api/type');
fetchDropdownData('availabilityDropdown', '/api/availability');
fetchDropdownData('ammoDropdown', '/api/ammo');
fetchDropdownData('colorDropdown', '/api/colors');
});
async function fetchDropdownData(dropdownId, url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch ${dropdownId} data`);
}
const data = await response.json();
populateDropdown(dropdownId, data);
} catch (error) {
console.error('Error:', error.message);
// Handle error
}
}
function populateDropdown(dropdownId, options) {
const dropdown = document.getElementById(dropdownId);
dropdown.innerHTML = ''; // Clear existing options
options.forEach(option => {
const optionElement = document.createElement('option');
optionElement.value = option;
optionElement.textContent = option;
dropdown.appendChild(optionElement);
});
}