Let only one checkbox checkbox dynamically

-1

I have a table, which I create dynamically, I need the user to be able to mark only a checkbox , that is when one has marked, at the time that it is marked another, it unchecks the other, and marks the new. I add this field in the table, like this:

  + "<td>" + "<input type='checkbox' class='link-check' />" + "</td>"
    
asked by anonymous 31.08.2018 / 22:20

1 answer

1

Translating the response from here (adapted):

+ "<td>" + "<input type='checkbox' class='link-check' onchange="cbChange(this)" />" + "</td>"

// ...

function cbChange(obj) {
  var cbs = document.getElementsByClassName("link-check");
  for (var i = 0; i < cbs.length; i++) {
     if(cbs[i] !== obj) cbs[i].checked = false;
   }
}

Basically, what was done was as follows: The function takes all checkboxes with class 'link-check', and places them in the cbs array. Itera on all of them, clearing all, and then, mark only the one that was clicked. Then add this function to an onchange event on all checkboxes to work on all of them.

I hope it helps.

    
31.08.2018 / 23:06