The function addRow will add row in the last child of a table, and deleteRow will delete checked row. The problem is, when I delete middle row, i.e. row 2 from 3 rows available, then the row 3 element's id still not updated even though there is only 2 rows remaining. How to update the row 3's id value?
function addRow() {
var tblList = document.getElementById("table1");
var tblBody = tblList.tBodies[0];
const lastRow = document.querySelector('#table1 tr:last-child td:first-child+td input') || 0;
const length = tblBody.rows.length;
try {
var row = tblBody.insertRow(length);
} catch (e) {
console.log(e);
return false;
}
var newCell0 = row.insertCell(0);
newCell0.innerHTML = `
<table style="width: 100%;">
<tr>
<td>
<input type="checkbox" id="eval_cb0_${length}" name="eval_cb0_${length}" value="checked" >
</td>
<td>
<label for="eval_cb0_${length}">EVAL 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="eval_cb1_${length}" name="eval_cb1_${length}" value="checked" >
</td>
<td>
<label for="eval_cb1_${length}">EVAL 2</label>
</td>
</tr>
</table>
`;
var newCell1 = row.insertCell(1);
newCell1.innerHTML = `
<input class="row-cb" type="checkbox" name="cb_clone[]">
`;
}
function deleteRow() {
const checkboxes = document.querySelectorAll(".row-cb");
const userConfirm = confirm("Are u sure?");
if (!userConfirm) return;
checkboxes.forEach((checkbox) => {
if (checkbox.checked) {
const row = checkbox.closest('tr');
row.remove();
}
});
}
<table id="table1">
<thead>
<tr>
<th>EVALUATION</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input id="button_delete" type="button" onclick="deleteRow()" value="DELETE">
<input id="button_add" type="button" onclick="addRow()" value="ADD">
The function addRow will add row in the last child of a table, and deleteRow will delete checked row. The problem is, when I delete middle row, i.e. row 2 from 3 rows available, then the row 3 element's id still not updated even though there is only 2 rows remaining. How to update the row 3's id value?
function addRow() {
var tblList = document.getElementById("table1");
var tblBody = tblList.tBodies[0];
const lastRow = document.querySelector('#table1 tr:last-child td:first-child+td input') || 0;
const length = tblBody.rows.length;
try {
var row = tblBody.insertRow(length);
} catch (e) {
console.log(e);
return false;
}
var newCell0 = row.insertCell(0);
newCell0.innerHTML = `
<table style="width: 100%;">
<tr>
<td>
<input type="checkbox" id="eval_cb0_${length}" name="eval_cb0_${length}" value="checked" >
</td>
<td>
<label for="eval_cb0_${length}">EVAL 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="eval_cb1_${length}" name="eval_cb1_${length}" value="checked" >
</td>
<td>
<label for="eval_cb1_${length}">EVAL 2</label>
</td>
</tr>
</table>
`;
var newCell1 = row.insertCell(1);
newCell1.innerHTML = `
<input class="row-cb" type="checkbox" name="cb_clone[]">
`;
}
function deleteRow() {
const checkboxes = document.querySelectorAll(".row-cb");
const userConfirm = confirm("Are u sure?");
if (!userConfirm) return;
checkboxes.forEach((checkbox) => {
if (checkbox.checked) {
const row = checkbox.closest('tr');
row.remove();
}
});
}
<table id="table1">
<thead>
<tr>
<th>EVALUATION</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input id="button_delete" type="button" onclick="deleteRow()" value="DELETE">
<input id="button_add" type="button" onclick="addRow()" value="ADD">
Share
Improve this question
edited Mar 17 at 17:08
isherwood
61.2k16 gold badges121 silver badges170 bronze badges
asked Mar 17 at 16:53
purple hippopurple hippo
594 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 5Frame challenge: This is an XY Problem. The real problem is that you want to create an input and a label, and use a for
attribute to link them. This requires that they have a unique ID.
Currently, you are counting the number of rows in the table to generate a number to use in the unique ID, and this is failing when you delete rows (because you already have an element with the ID generated when there were previously a smaller number of rows).
To solve this don't use the length in the ID, use some other value.
A simple solution would be to create a new variable:
let idCounter = 0; // Scoped outside the function so it persists between calls
function addRow() {
and then whenever you need to generate an ID
const myId = `eval_cb0_${++idCounter}`;
If you wanted unique IDs which could persist across pages, then you could look at using a library like uuid instead.
id
property of an element... – Sergey A Kryukov Commented Mar 17 at 16:59id
modification is bad, it would be too boring... – Sergey A Kryukov Commented Mar 17 at 17:53