2

I have an usercontrol which should be disabled/enabled based on a dropdown selection.Please find below the piece of code.

  if (name== 'A') {
        $("#uc_panel").prop("disabled", "disabled");
        $("#uc_btnAddCategory").prop("disabled", "disabled");
        $("#uc_ddlCategorySelection").prop("disabled", "disabled");}
  else
  {
    $("#uc_panel").prop("disabled", "");
    $("#uc_btnAddCategory").prop("disabled", "");
    $("#uc_ddlCategorySelection").prop("disabled", "");
  }

1st time I select 'A' from the dropdown , usercontrol is disabled. 2nd time I select 'B' and click on BtnAdd, the usercontrol is getting disabled but it should be enabled

2 Answers 2

1

If you are using prop function, use boolean as values when props are 'disabled', 'checked', etc. You can check documentation. The way you are using prop is same as was used in older versions of jquery with attr function

if (name == 'A') {
    $("#uc_panel").prop("disabled", true);
    $("#uc_btnAddCategory").prop("disabled", true);
    $("#uc_ddlCategorySelection").prop("disabled", true);
} else {
    $("#uc_panel").prop("disabled", false);
    $("#uc_btnAddCategory").prop("disabled", false);
    $("#uc_ddlCategorySelection").prop("disabled", false);
}

So you can shorter it like this:

var isA = name == 'A';
$("#uc_panel").prop("disabled", isA);
$("#uc_btnAddCategory").prop("disabled", isA);
$("#uc_ddlCategorySelection").prop("disabled", isA);

or:

$("#uc_panel, #uc_btnAddCategory, #uc_ddlCategorySelection").prop("disabled", name == 'A')
Sign up to request clarification or add additional context in comments.

Comments

0

This Help You.

HTML

<select id="mySelect" onchange="checkField(this.value)">
  <option>Apple</option>
  <option>Banana</option>
  <option>Orange</option>
</select>

<input type="text" id="txt" value="Hello">

Javascript

<script>
   function checkField(val)
    {          
      var button = document.getElementById("txt");
      button.disabled = true;
    }
</script>

On Drop Down change it will disable the textbox

Moreover, In order to respond of the change of the selected index, add this onchange event to the dropdown: onchange="disableButton()"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.