Option disabled Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Option

Description

The disabled property sets or gets whether an option in a drop-down list should be disabled.

Set the disabled property with the following Values

Value Description
true|false Sets whether an option in a drop-down list should be disabled

Return Value

A Boolean, returns true if the option is disabled, otherwise it returns false

The following code shows how to Disable the third option (index 2) in a drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="pets" size="3">
  <option>Cat</option>
  <option>Dog</option>
  <option>Horse</option>
</select>/*from   w  ww  . j  a  v a2s  . c o  m*/

<button onclick="myFunction()">Disable Option</button>

<script>
function myFunction() {
    var x = document.getElementById("pets").options[2].disabled = true;
}
</script>

</body>
</html>

Related Tutorials