How to make some options non-selectable? #6079
Unanswered
binarykitchen
asked this question in
Q&A
Replies: 1 comment
-
|
Yes, it’s possible, but it depends a bit on what UI / library you’re using. In plain HTML, there’s no official way to make an option non-selectable without using That said, here are the usual approaches you can take.
HTML only supports:
So if you do this: <option disabled>Extra information</option>it will always be greyed out by the browser. There’s no standard attribute for “non-selectable but normal-looking”.
<select id="mySelect">
<option value="">Extra information (not selectable)</option>
<option value="a">Option A</option>
<option value="b">Option B</option>
</select>
<script>
const select = document.getElementById("mySelect");
select.addEventListener("change", (e) => {
if (e.target.value === "") {
e.target.selectedIndex = -1; // or reset to previous value
}
});
</script> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone
It is possible to flag some options as non-selectable?
Similar to
disabledbut don't want to grey them out. This for decoration purposes, to add more information for the user. Very similar to the titles of grouped options but not grouped.If you know how to, please share. Thanks for any tips 🙇
Beta Was this translation helpful? Give feedback.
All reactions