Rasul
Rasul

Reputation: 71

How to disable dropdown when specific option selected

I need to disable dropdown when specific option selected. For example in bottom code when option with Deleted value selected, dropdown disabled.

<select name="status[]">
    <option value="In Use">In use</option>
    <option value="Deleted">Deleted</option>
</select>

How can i do this work using jquery?

Upvotes: 3

Views: 854

Answers (2)

Mohammad
Mohammad

Reputation: 21489

Use this code

$("#dropdown").change(function() {
    if ($("option:selected", this).val() == "Delete")
        $(this).attr("disabled", true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
    <option value="Option-1">Option-1</option>
    <option value="Option-2">Option-2</option>
    <option value="Option-3">Option-3</option>
    <option value="Delete">Delete</option>
</select>

Upvotes: 2

Gerard
Gerard

Reputation: 15786

The code below does the trick

$(document).on("change", "select", function() {
  if ($("select option:selected").val() == "Deleted") {
    $(this).attr("disabled", "disabled");
  }
})

Upvotes: 0

Related Questions