Check if value is in dropdown list and disable it in Jquery
-
25-05-2021 - |
Question
How can i check if an item is in a select drop down list and disable it if it fulfills some condition?
Thanks in advance!
BTW i tried the following and nothing is happening
$('#select-box option').each(function()
{if (this.val() == item)
{$('#select-box option').attr("disabled","disabled");}});
EDITED*
The second code i used while debugging. It went through smoothly until i did the comparison with item. I am wondering if it is because of the this value. If so how do i overcome it?
function checkSelectBox(item){ <!--alert(item);-->
$('#SCOPE option').each(function()
{
if($(this).val()==item)
{ alert('Camein')
$(this).attr("disabled", "disabled");
return false;
}
});
}
Solution
Do you mean:
$('#yourSelectId option').each(function(){
if ($(this).val() == 'some_value_to_check') {
$(this).attr("disabled", "disabled");
return false;
}
});
OTHER TIPS
Hey this is working example: http://jsfiddle.net/R6GAr/11/
HTML: Sample
<SELECT NAME="SCOPE" id="SCOPE">
<OPTION VALUE="G"> Global
<OPTION VALUE="D" selected="selected"> Dynamic
</SELECT>
JQuery: sample
$('#SCOPE').change(function(){
if($(this).val() == 'G') {
// **disables the option here with attr as below**
$('#SCOPE option[value="'+$(this).val()+'"]').attr("disabled","disabled");
// **Now you want that disable to be not selectable as well, this line does that.**
$('#SCOPE option[value="'+$(this).val()+'"]').attr("selected",false);
}
});
Hope this helps, cheers!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow