A dropdown can be used as a navigation control by mapping known option values to known URLs and changing window.location.href after the user chooses a destination. For ordinary site navigation, visible links are usually easier to discover and use; use a select menu when the compact control genuinely fits the interface.
const routes = {
PHP: "../php_tutorial/site_map.php",
MySQL: "../sql_tutorial/site_map.php",
JS: "site_map.php"
};
Demo of dropdown page redirection
const select = document.getElementById("s1");
select.addEventListener("change", () => {
const destination = routes[select.value];
if (destination) {
window.location.href = destination;
}
});
The original tutorial used getElementById() and a switch statement. A switch still works, but an object map keeps the relationship between values and URLs compact.
Map a fixed set of option values to destinations you control. Do not accept a free-form URL from the visitor and assign it directly to window.location.
The old script redirected unmatched values to the home page. The revised example simply does nothing when the placeholder or an unknown value is selected, avoiding an unexpected navigation.
The original tutorial also combines redirection with the dependent dropdown example. Choose “Scripts” in the first list and a supported technology in the second list to navigate.
Demo: dependent dropdown with redirectsubcategory.addEventListener("change", () => {
const destination = routes[subcategory.value];
if (destination) {
window.location.href = destination;
}
});
If the user needs to compare destinations before choosing, a list of normal links is more direct. If the dropdown is part of a form, consider a submit button instead of navigating immediately on selection so the action is explicit.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Tom | 10-02-2014 |
| Thank you for this amazing code. Now I am trying to figure out how to add a third list box by adding another function to the code. Is it even possible? Any help would be greatly appreciated? | |
| Vikas Jangra | 24-04-2015 |
| thanks a lot....very useful and logical coding | |
03-09-2020 | |
| Thanks for this content, this is a very useful | |
20-06-2023 | |
| Thank you for this amazing code. Now I am trying to figure out how to add a third list box by adding another function to the code. Is it even possible? Any help would be greatly appreciated? | |
22-07-2023 | |
| Yes, it should be possible, but better to use JQuery here. we will try. | |