Using href links inside <option>
tag
To **navigate using dropdowns**, HTML **doesn't support** `href` in `<option>`.
**Use JavaScript**: on `<select>` change, **redirect** to the option's value as the URL.
**Example**:
```html
<select onchange="if (this.value) window.location.href=this.value;">
<option value="http://example.com/page1">Go to Page 1</option>
<option value="http://example.com/page2">Go to Page 2</option>
</select>
This triggers a redirect to the selected page.
Why not ``<option> hrefs
? Semantic understanding
The <select>
element is purpose-built for form selections. Using it for navigation bends its semantic purpose and can reduce usability, particularly for screen readers and keyboard navigation.
The right tool: <a>
within <li>
Instead of (mis)using <select>
, create a dropdown menu using <ul>
and <li>
that contain <a>
tags. This approach keeps the semantics intact and improves accessibility.
Making <select>
behave using onchange
While <select>
shouldn't be used for navigation, you might use it for actions like sorting or filtering. In such scenarios, onchange
can be useful to respond to user selection.
Handling reselections and navigating with care
Note that the onchange
event will not trigger if the user reselects the same option. To handle this, you can harness data attributes or manage state with JavaScript.
AJAX and jQuery: Advanced navigation techniques
For UX enhancement, consider AJAX for loading new content without a full page refresh.
And jQuery simplifies window navigation on dropdown change:
<select>
as a presentation changer
Though not ideally employed for navigation, the <select>
function can be used effectively for modifying presentation like sorting or toggling views. Ensure your UI/UX design aligns with user expectations.
Embracing accessibility in navigation
In all your navigational designs, don't forget about accessibility. Real links (<a>
tags) ensure that your application supports screen readers and keyboard navigation. Here, user diversity matters.
Was this article helpful?