lundi 29 juillet 2019

How can you trigger a function if one checkbox out of multiple checkboxes is clicked

I am creating a Todo app and I need to trigger a function (which counts number of checkboxes checked) when any one of the checkboxes is checked.

I am unable to get an onlick event to happen if a checkbox is clicked. I have manage to do it with a submit button, but not with the checkbox itself

//the html
<div class="flow-right controls">
        <span>Item count: <span id="item-count">0</span></span>
        <span>Unchecked count: <span id="unchecked-count">0</span></span>
      </div>
      <button class="button center" onClick="newTodo()">New TODO</button>
      <ul id="todo-list" class="todo-list"></ul>
    </div>

// the function above this one creates the checkbox and appends it to the list in the HTML
const box = document.createElement('INPUT');
      box.type = "checkbox";
      box.name = "countme";
      box.id = "checkme"
      li.appendChild(box);

// this is the code I have created to trigger a function unchecked which returns the count of unchecked checkboxes.

let getcheck = document.getElementsByName("countme");
for (let i = 0; i < getcheck.length; i++) {
   getcheck[i].onClick = unchecked;
 }

Nothing is happening so I am unsure with how to debug this




Aucun commentaire:

Enregistrer un commentaire