How to check if dropdown is disabled?

Jquery

Jquery Problem Overview


Using jquery how do i simply check if it is read only?

this is what i am trying..

$("#item").keydown(function (event) {
     //alert(event.keyCode);
     if (event.keyCode == 13) {
         $("#ok").click();               
         if ($('#dropLength').prop("disabled") == false) {
             $("#dropLength").focus();
             return;
         }
         if ($('#dropUnit').prop("disabled") == false) {
             $("#dropUnit").focus();
             return;
         }
         $("#qty").focus();                
         return ;
     }
 });

The dropdowns are set to readonly using jquery also:

if ($('#dropLength').find('option').length <= 1) {
      $('#dropLength').attr("disabled", "disabled");
}
if ($('#dropUnit').find('option').length <= 1) {
      $('#dropUnit').attr("disabled", "disabled");
}   

Jquery Solutions


Solution 1 - Jquery

The legacy solution, before 1.6, was to use .attr and handle the returned value as a bool. The main problem is that the returned type of .attr has changed to string, and therefore the comparison with == true is broken (see http://jsfiddle.net/2vene/1/ (and switch the jquery-version)).

With 1.6 .prop was introduced, which returns a bool.

Nevertheless, I suggest to use .is(), as the returned type is intrinsically bool, like:

$('#dropUnit').is(':disabled');
$('#dropUnit').is(':enabled');

Furthermore .is() is much more natural (in terms of "natural language") and adds more conditions than a simple attribute-comparison (eg: .is(':last'), .is(':visible'), ... please see documentation on selectors).

Solution 2 - Jquery

Try following or check demo disabled and readonly

$('#dropUnit').is(':disabled') //Returns bool
$('#dropUnit').attr('readonly') == "readonly"  //If Condition

You can check jQuery FAQ .

Solution 3 - Jquery

There are two options:

First

You can also use like is()

$('#dropDownId').is(':disabled');

Second

Using == true by checking if the attributes value is disabled. attr()

$('#dropDownId').attr('disabled');

whatever you feel fits better , you can use :)

Cheers!

Solution 4 - Jquery

I was searching for something like this, because I've got to check which of all my selects are disabled.

So I use this:

let select= $("#select");

for (let i = 0; i < select.length; i++) {
    const element = select[i];
    if(element.disabled == true ){
        console.log(element)
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeginnerView Question on Stackoverflow
Solution 1 - Jqueryuser57508View Answer on Stackoverflow
Solution 2 - JqueryAmar PalsapureView Answer on Stackoverflow
Solution 3 - JqueryManish ShrivastavaView Answer on Stackoverflow
Solution 4 - JqueryLucas FloresView Answer on Stackoverflow