Disabling elements in dropdown

Dear community,

I created a dropdown menu with the Select HTML code.

How can I disable all element at once, without looping through them?

I tried the following, but it leads to the dropdown being disabled, rather than its individual elements:

var select = document.getElementById(“myDropdown”);
select.setAttribute(“disabled”, " ");

Kind regards and many thanks

To disable all options within a <select> dropdown without disabling the dropdown itself, you indeed need to apply the disabled attribute to each <option> element.

Unfortunately, JavaScript doesn’t provide a direct way to do this without iterating through them.

You can do it in a relatively concise one-liner, though:

document.querySelectorAll('#myDropdown option').forEach(option => option.disabled = true);
1 Like

Yes but for what should this be good? You waste the users time to click on something he cannot use….

1 Like

This is great help, many thanks!

it actually is part of a bigger problem. I first disable all elements, and then enable some elements for which specifc conditions hold