0

I have a script that toggles checkbox when page is loaded (by default it's unchecked).

Example:

if (bla bla bla) {
    $('#test').trigger('click');
}

It works as expected, but if I go forward to the next page and then go back again (or restore closed tab with that page), this doesn't work: the checkbox remains unchecked. I guess in this case browser doesn't execute js.

Is there any solution to resolve this issue? Thanks.

  • 3
    Possible duplicate of [Cross-browser onload event and the Back button](http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button) – Lee Apr 08 '16 at 10:35

1 Answers1

1

When you go back, (most browsers) reload the form as it was when you left the page, and then toggle the checkbox.

The best solution here might be to set the checkbox to checked rather than toggling it.

$('#test').prop('checked', true);
CooncilWorker
  • 395
  • 2
  • 12