Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dropdownlist disabled on change event using JQUERY?

$(document).ready(function() {
      $('#<%=ddlContinents.ClientID %>').change(function() { 
      var element = $(this);
      var totalLength = element.children().length; 
      if ($(this).disabled == false) { $(this).disabled = true; }
      });
});

What I am trying to do is fire off the change event of the dropdownlist and on change making this dropdownlist disabled. The code is firing and everything, but it does not disable the dropdownlist.

This portion of the code is not working:

if ($(this).disabled == false) { $(this).disabled = true; } });
like image 267
Shiva Avatar asked Mar 12 '09 18:03

Shiva


2 Answers

You should use .prop() for jQuery 1.6+ or .attr() for earlier versions of jQuery:

> jQuery 1.6:

$(document).ready(function() {
  $('#<%=ddlContinents.ClientID %>').change(function() { 
    var element = $(this);
    var totalLength = element.children().length;

    if (!$(this).prop("disabled")) { 
      $(this).prop("disabled", true); 
    } 
  });
});

< jQuery 1.6:

$(document).ready(function() {
  $('#<%=ddlContinents.ClientID %>').change(function() { 
    var element = $(this);
    var totalLength = element.children().length;

    if (!$(this).attr("disabled")) { 
      $(this).attr("disabled", "disabled"); 
    } 
  });
});
like image 109
Erik L Avatar answered Oct 07 '22 04:10

Erik L


if (!$(this).attr("disabled")) { $(this).attr("disabled","disabled"); }

If you want to enable it later on, you gotta do:

$(this).removeAttr("disabled");
like image 44
Paolo Bergantino Avatar answered Oct 07 '22 04:10

Paolo Bergantino