1

In first page have several check boxes, I select one of them, and press submit button. Submit button moves me to second page, on that second page if I press back button in the browser, I came back to the first page where the same check box is selected.

And question is how reset that check box (or all check boxes)?

I can't reset it then I click button bescause then I lost post data with php.

piotr
  • 45
  • 1
  • 7

2 Answers2

1

Use javascript body onunload event

<body onunload="clearCheckBoxes()">

and write clearCheckBoxes function to clear all checkboxes.

function clearCheckBoxes() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++){
        if (inputs[i].type === 'checkbox')
            inputs[i].checked = false;
    }
}

In order to work for IE use onbeforeunload event of window instead of the body onunload event:

window.onbeforeunload = clearCheckBoxes;
Mohsenme
  • 1,012
  • 10
  • 20
  • So if the form is submitted, the onunload is sure to run after the onsubmit so the checkboxes are still sent to the server correctly? – mplungjan Jan 26 '14 at 16:17
  • Now I tested it on IE and in IE it is not working? Maybe You know why? – piotr Jan 26 '14 at 18:20
  • You're right. I also used <= in _for_ statement condition clause incorrectly which should be < only. – Mohsenme Jan 26 '14 at 19:24
0

Perhaps this at the bottom of the page:

<script>
var chk = document.getElementById("someCheckboxID");
chk.checked=chk.defaultChecked; // set to default
</script>
</body>
mplungjan
  • 169,008
  • 28
  • 173
  • 236