Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enable disabled select input with JavScript

I would like to enable a select input that is disabled using JavaScript.

I have tried doing this using:

document.querySelector("#radius").disabled = false;

And also:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

document.querySelector("#radius").removeAttribute("disabled");

Neither of these has worked.

The markup for the select input is:

<select name="radius" id="radius" readonly>
   <option value="all">All distances</option>
      <optgroup label="Miles">
        <option value="5mi" disabled>5 miles</option>
        <option value="10mi" disabled>10 miles</option>
        <option value="20mi" disabled>20 miles</option>
    </optgroup>
    <optgroup label="Kilometers">
        <option value="5km" disabled>5 kilometers</option>
        <option value="10km" disabled>10 kilometers</option>
        <option value="20km" disabled>20 kilometers</option>
    </optgroup>
</select>

>Solution :

It looks like the options are disabled, but you are trying to enable the select itself. The queryselector modifies the object that you are selecting and not its children. They have to be referred to separately.

Here is a javascript loop that sets the options as not disabled.

Also According to MDN about read only

The attribute is not supported or relevant to select or input types that are already not mutable, such as checkbox and radio or cannot, by definition, start with a value, such as the file input type. range and color, as both have default values. It is also not supported on hidden as it can not be expected that a user to fill out a form that is hidden. Nor is it supported on any of the button types, including image.

let opts = document.querySelectorAll("#radius [disabled]");

opts.forEach(function(e){
 e.disabled = false;
});
<select name="radius" id="radius" readonly>
   <option value="all">All distances</option>
      <optgroup label="Miles">
        <option value="5mi" disabled>5 miles</option>
        <option value="10mi" disabled>10 miles</option>
        <option value="20mi" disabled>20 miles</option>
    </optgroup>
    <optgroup label="Kilometers">
        <option value="5km" disabled>5 kilometers</option>
        <option value="10km" disabled>10 kilometers</option>
        <option value="20km" disabled>20 kilometers</option>
    </optgroup>
</select>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading