1

I want to disable a dropdown list on page load and enable it on button click event using JQuery. I have tried the below code but its not working. What is happening is it is disabling the list on page load, then enabling the list on button click event for one second and then again disabling the list.

$(document).ready(function () {
 debugger;
if (jQuery('#btnViewHistoricData').data('clicked')) {
 $("#ddlBranch").prop("disabled", false);
} else {
 $("#ddlBranch").prop("disabled", true);
}
});

 <div class="text-center">
<button id="btnViewHistoricData" class="btn bg-dark">View Historic Data</button>
</div>

<div class="col-md-3">
<div class="form-group m-r-sm">
<label for='rf_name'>Branch</label>
<select id="ddlBranch"  class="m-b-sm  w-lg form-control" onchange="ViewHistoricData()">
<option value="ALL" selected="selected">ALL</option>
</select>
</div>

3
  • if you get rid of the else block, does that help? Commented Jul 31, 2017 at 14:05
  • i want to disable the list on page laod and enable it on button click, thats why i have chosen if else condition Commented Jul 31, 2017 at 14:08
  • whenever btnViewHistoricData is not pressed, the list is disabled Commented Jul 31, 2017 at 14:51

3 Answers 3

1

When you click the button you will do a form post, causing the document to reload and the dropdownlist to be set to disabled.

So make the button of type=button

<button id="btnViewHistoricData" class="btn bg-dark" type="button">View Historic Data</button>

Now when the button is clicked there will be no form post so the DropDownList can be enabled.

<script type="text/javascript">
    $(document).ready(function () {
        $("#ddlBranch").prop("disabled", "disabled");

        $("#btnViewHistoricData").click(function () {
            $("#ddlBranch").removeAttr("disabled");
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code.

$(document).ready(function () {

$("#btnViewHistoricData").click(function(){
  $("#ddlBranch").removeAttr("disabled");

});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text-center">
<button id="btnViewHistoricData" class="btn bg-dark">View Historic Data</button>
</div>

<div class="col-md-3">
<div class="form-group m-r-sm">
<label for='rf_name'>Branch</label>
<select id="ddlBranch"  class="m-b-sm  w-lg form-control" onchange="ViewHistoricData()" disabled="disabled">
<option value="ALL" selected="selected">ALL</option>
</select>
</div>

4 Comments

at the end its again disabling the list
I am not getting you can you please explain more?
after enabling the dropdown list for fraction of second, its again disabling the dropdown list
Disable only happens when you refresh the page you can check by running code snippet. After fraction of seconds it is not getting disable.
0
$(document).ready(function () {
 debugger;
if (jQuery('#btnViewHistoricData').on('clicked')) {  // Use `on` instead
 $("#ddlBranch").prop("disabled", false);
} else {
 $("#ddlBranch").removeAttr("disabled");
}
});

1 Comment

not working as expected. Its not disabling the list on page load

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.