I use JavaScript to create multiple checkboxes to be inserted into a table. Each checkbox is in one row.
I give all of them the same name and class name.
But the problem is I can not create a function to be fired when I click that element.
What I want to do is create a function to be fired by clicking on any created checkbox.
btnSave.onclick = function() {
var table = document.getElementById("table");
var row = table.insertRow(document.getElementById("table").rows.length);
var cellCHECK = row.insertCell(0);
var cellID = row.insertCell(1);
var cellNAME = row.insertCell(2);
var cellAGE = row.insertCell(3);
var checkbox = document.createElement("INPUT");
checkbox.type = "checkbox";
checkbox.name = "check";
checkbox.className = "check";
checkbox.id = document.getElementById("table").rows.length - 1;
checkbox.value = document.getElementById("table").rows.length - 1;
cellCHECK.appendChild(checkbox);
}
<table id="table">
<tr>
<th></th>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</table>
<button class="btnSave" id="btnSave">Save</button>