I want to have my start shopping button enabled after a user selects a state and school, then have the link for that button direct to the url of the school they selected on the dropdown. Right now, the button will just reload the page I'm on, rather than take me to the page of the school they selected.
// Define schools data
const schools = [
{ name: "HLS Louisville", url: "https://highlandsuniforms.com/louisville/", state: "KY" },
{ name: "HL Cottage School Louisville", url: "https://highlandsuniforms.com/hlcs-louisville/", state: "KY" },
{ name: "HLS Beaufort", url: "https://highlandsuniforms.com/beaufort/", state: "SC" },
{ name: "HLS Billings, MT", url: "https://highlandsuniforms.com/hls-billings-mt/", state: "MT" },
{ name: "HLS Charleston, SC", url: "https://highlandsuniforms.com/charleston-sc/", state: "SC" },
{ name: "HLS Summerville, SC", url: "https://highlandsuniforms.com/highlands-latin-school-summerville/", state: "SC" },
{ name: "HLS Ft. Myers, FL", url: "https://highlandsuniforms.com/hls-ft-myers-fl/", state: "FL" },
{ name: "HLS Houston", url: "https://highlandsuniforms.com/hls-houston/", state: "TX" },
{ name: "HLS Indianapolis", url: "https://highlandsuniforms.com/indianapolis", state: "IN" },
{ name: "HLS Orlando", url: "https://highlandsuniforms.com/orlando/", state: "FL" },
{ name: "HLS Southern California", url: "https://highlandsuniforms.com/southern-california", state: "CA" },
{ name: "Archangels Academy", url: "https://highlandsuniforms.com/archangels", state: "IL" },
{ name: "Lexington Latin School", url: "https://highlandsuniforms.com/lexington-latin", state: "KY" },
{ name: "Northstar Classical School", url: "https://highlandsuniforms.com/northstar-classical-school/", state: "MN" }
];
// Get DOM elements
const stateSelect = document.getElementById("state-select");
const schoolSelect = document.getElementById("school-select");
const startShoppingButton = document.getElementById("start-shopping");
// Populate state options
const states = [...new Set(schools.map(school => school.state))];
states.forEach(state => {
const option = document.createElement("option");
option.value = state;
option.textContent = state;
stateSelect.appendChild(option);
});
// Reset school options when state changes
stateSelect.addEventListener("change", function () {
// Reset school options
schoolSelect.innerHTML = "<option value=''>Select a school</option>";
startShoppingButton.disabled = true;
// Populate school options for selected state
const selectedState = stateSelect.value;
const schoolsInState = schools.filter(school => school.state === selectedState);
schoolsInState.forEach(school => {
const option = document.createElement("option");
option.value = school.url;
option.textContent = school.name;
schoolSelect.appendChild(option);
});
});
// Enable start shopping button when a school is selected
schoolSelect.addEventListener("change", function () {
if (schoolSelect.value) {
startShoppingButton.disabled = false;
} else {
startShoppingButton.disabled = true;
}
});
// Redirect to selected school's URL when "Start Shopping" button is clicked
startShoppingButton.addEventListener("click", function () {
const selectedSchool = schools.find(school => school.state === stateSelect.value && school.name === schoolSelect.value);
if (selectedSchool) {
window.location.href = selectedSchool.url;
}
});
label {
font-size: 20px;
color: black;
display: inline-block;
margin-bottom: 10px;
}
select {
font-size: 20px;
padding: 10px;
border-radius: 0;
border: 2px solid black;;
background-color: white;
color: black;
width: 250px;
display: inline-block;
margin-right: 35px;
margin-left: 35px;
}
/* Style for the buttons */
button {
font-size: 5px;
padding: 15px 30px;
border-radius: 0;
border: none;
background-color: grey;
color: white;
cursor: pointer;
transition: background-color 0.3s, color 0.3s;
display: inline-block;
}
/* Style for the start shopping button */
#start-shopping {
font-size: 20px;
padding: 10px;
border-radius: 0;
border: none;
background-color: black;
color: white;
cursor: pointer;
transition: background-color 0.3s, color 0.3s;
width: 250px;
display: inline-block;
margin-right: 35px;
margin-left: 35px;
}
<form>
<label for="state-select"></label>
<select id="state-select">
<option value="">Select a state</option>
</select>
<label for="school-select"></label>
<select id="school-select">
<option value="">Select a school</option>
</select>
<button id="start-shopping" disabled>Start Shopping</button>
</form>