How to check if dropdown is disabled?
JqueryJquery 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)
}
}