Dean.DePue
Dean.DePue

Reputation: 1013

Disable all option of listbox except one selected

I have a listbox with a list of countries and 'ALL COUNTRIES' at the top. I want to disable all of the items except the first if it is selected. I cannot find a way to do this and leave the first item enabled. I also want to be able to click on the first again and have it unselect - is there a way to do this?

 $("#lbCountries").click(function () {
     $("#lbCountires option").each(function (index) {
        if ($(this).is(':selected') && $(this.val() == 'AL') {
            $(this).prop('disabled', false);
         }
         else {
            $(this).prop('disabled', true);
         }
      });
  });

They all get disabled, including the first one. How can I stop that from happening and be able to click on it again and reverse what has been done?

Upvotes: 0

Views: 4895

Answers (1)

Amin Jafari
Amin Jafari

Reputation: 7207

have you tried:

$("#lbCountries").click(function () {
     $("#lbCountires option").not(':first-child').each(function (index) {
        $(this).prop('disabled', true);
      });
  });

Upvotes: 2

Related Questions