Disable the third option (index 2) in a drop-down list:
document.getElementById("pets").options[2].disabled = true;
Click the button to disable the third option (index 2) in the dropdown list.
<!DOCTYPE html> <html> <body> <select id="pets" size="3"> <option>CSS</option> <option>Java</option> <option>HTML</option> </select>/*from w ww . jav a2 s . co m*/ <button onclick="myFunction()">Disable Option</button> <script> function myFunction() { var x = document.getElementById("pets").options[2].disabled = true; } </script> </body> </html>
The disabled property sets or gets whether an option in a drop-down list should be disabled.
The disabled property accepts and returns a boolean type value.
Value | Description |
---|---|
true | The option is disabled |
false | Default. The option is not disabled |
The disabled property returns true if the option is disabled, otherwise it returns false.