javascriptjqueryjquery-attributes

Check if value is in dropdown list and disable it in Jquery


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;
        }
    });