The code ($("#restprovider").click();
) works in Chrome but not in e.g. Firefox v.58.
The issue appears to be related to the fact that Firefox doesn't recognize the checkbox
as enabled until after a redraw is made.
One option to fix that is to wrap the click()
call in a setTimeout()
, as shown in below sample, but there are several more options:
Note, the here used timeout value of 4ms might not work cross browser, so its value might has to be adjusted a little.
Stack snippet
function disableCheckbox() {
$("#restprovider").prop("disabled", true);
}
function enableCheckboxAndClick() {
$("#restprovider").prop("disabled", false);
setTimeout(function() {
$("#restprovider").click();
}, 4)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="restprovider" type="checkbox">
<button onclick="disableCheckbox()">
disable checkbox
</button>
<button onclick="enableCheckboxAndClick()">
enable and click checkbox
</button>
Updated after a question edit
If you are only interested e.g. in toggling the input
's checked state, simply do like this:
function disableCheckbox() {
$("#restprovider").prop("disabled", true);
}
function enableCheckboxAndClick() {
$("#restprovider").prop("disabled", false);
$("#restprovider").prop("checked", function() { this.checked = !this.checked });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="restprovider" type="checkbox">
<button onclick="disableCheckbox()">
disable checkbox
</button>
<button onclick="enableCheckboxAndClick()">
enable and click checkbox
</button>