mercredi 8 mars 2017

Javascript issue with check all checkboxes on load

I have a button that when clicked will select / deselect all checkoxes that are output on the page. This works great. I want them to load deselected and when the button is clicked they go selected. Right now I can only get it to work by having them load selected. I have tried messing around with this code but I cannot get these to load deselected. I'm not great with javascript. Can anyone see how I would do that with this code? Or do I have to do it a different way?

var checkflag = "true";
function check(field) {
  if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
    checkflag = "true";
    return "Deselect All";
  }
  else {
    for (i = 0; i < field.length; i++) {
      field[i].checked = false;
    }
    checkflag = "false";
    return "Select All";
  }
}

<input class="btn btn-large btn-primary btn-block" type="button" name="CheckAll" value="Deselect All" onClick="this.value=check(this.form,true)"     style="width:115px;">




Aucun commentaire:

Enregistrer un commentaire