views:

488

answers:

2

I have the following JavaScript to disable a dropdownlist in a ASP.NET page , which gets called when I click a button.

function disableDropDown(DropDownID)
{
  document.getElementById(DropDownID).disabled = true;
  return false; 
}

I wanted to use the same button to toggle between enable and disable for that dropdownlist. How do I do this?

+2  A: 

You just have to invert the boolean disabled attribute:

function toggleDisableDropDown(dropDownID) {
  var element = document.getElementById(dropDownID); // get the DOM element

  if (element) { // element found
    element.disabled = !element.disabled; // invert the boolean attribute
  }

  return false; // prevent default action
}
CMS
A: 
function toggleDisableDropDown(DropDownID)
{
  var sel = document.getElementById(DropDownID);
  sel.disabled = !sel.disabled;
  return false; 
}
Jage