jeudi 2 avril 2020

Check all / check none checkboxes

I' trying to establish user options for checking all and none checkboxes. I use this code:

function selectToggle(toggle, form) {
     var myForm = document.forms['cbx'];
     for( var i=0; i < myForm.length; i++ ) { 
          if(toggle) {
               myForm.elements[i].checked = "checked";
          } 
          else {
               myForm.elements[i].checked = "";
          }
     }
}

document.getElementById("all").addEventListener("click", selectToggle, true);
document.getElementById("none").addEventListener("click", selectToggle, false);

Here works only the option check all - check none doesn't work.

But, if i use instead of event listeners inline javascript like <a href="javascript:selectToggle(true, 'cbx');">All</a> | <a href="javascript:selectToggle(false, 'cbx');">None</a> both options work.

How can i force document.getElementById("none").addEventListener("click", selectToggle, false); to work?




Aucun commentaire:

Enregistrer un commentaire