0

I try to enable a checkbox and then click it via code. But it doesn't work. If I click the enable and click checkbox button the checkbox toggles. But if I press disable checkbox first it only gets enabled and not clicked by pressing enable and click checkbox button.

function disableCheckbox() {
 $("#restprovider").prop("disabled", true);
}
function enableCheckboxAndClick() {
 $("#restprovider").prop("disabled", false);
  $("#restprovider").click();
}
<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>
ochs.tobi
  • 3,214
  • 7
  • 31
  • 52
  • 2
    Works for me in Chrome – Asons Mar 23 '18 at 08:24
  • By click the checkbox do you mean you want to set it to checked? A click is not the best way to do that - you can just set it to checked – StudioTime Mar 23 '18 at 08:24
  • Working on Chrome 65. – Zenoo Mar 23 '18 at 08:24
  • And not in Firefox...so this is a browser issue – Asons Mar 23 '18 at 08:25
  • working in firefox too @LGSon – Rupal Mar 23 '18 at 08:26
  • Well, not in mine, version 58.0.2 – Asons Mar 23 '18 at 08:27
  • Thanks for the quick response. It is truly a browser issue. – ochs.tobi Mar 23 '18 at 08:29
  • I can do this with `$("#restprovider").prop("checked", true/false);`, too. But that would not fire the onclick method from the checkbox – ochs.tobi Mar 23 '18 at 08:34
  • I just noticed your question edit. First: It is not only _older Firefox versions_ this happens to, I use their latest and it has the same issue. ... Second: Is it the `onclick` event you want to fire for the input or just toggle its checked status? ... Am asking, as if it's to really fire the event, the solution the accepted answer shows won't work either (seen here [https://jsfiddle.net/cr29y1tc/382/](https://jsfiddle.net/cr29y1tc/382/)), for the same reason I explained in my answer. If it is to set or toggle the state, then I updated my answer with a much simpler solution to accomplish that. – Asons Mar 25 '18 at 17:14

5 Answers5

1

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>
Asons
  • 84,923
  • 12
  • 110
  • 165
1

Instead of

  $("#restprovider").click();

Use

 $("#restprovider").prop( "checked", true );

Now It won't toggle.

function disableCheckbox() {
 $("#restprovider").prop("disabled", true);
}
function enableCheckboxAndClick() {
 $("#restprovider").prop("disabled", false);
  $("#restprovider").prop( "checked", true );
}
<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>
Surya Neupane
  • 906
  • 10
  • 20
0

One way is to use checked property instead:

var $restprovider = $('#restprovider');

function disableCheckbox() {
    $restprovider.prop("disabled", true);
}

function enableCheckboxAndClick() {  
    $restprovider.prop('disabled', false);
    $restprovider[0].checked 
        ? ($restprovider.click(), $restprovider.prop('checked', false)) 
        : $restprovider.prop('checked', true);
}

And here's an example.

Roland Ruul
  • 1,172
  • 8
  • 15
-1

your code works good. you can resimple code with:

$("#restprovider").prop("disabled", false).click();
Cuong Do
  • 29
  • 3
-1

If you want a checkbox that looks like it is disabled, you can do it like that:

$('#restprovider').css({pointerEvents:"none", opacity:0.5});