I want to add a <td> to every row when a button gets clicked with javascript but I want it to be a toggle button, when it gets clicked the second time it has to disappear again.
I tried doing a for loop on the children of the table tag but that adds a table row, i also tried giving them all the same class but that only adds the to the first element with that class.
This is where I add the table info
const verwerkDatatable = function(data) {
console.log(data);
const table = document.querySelector('.js-table');
table.innerHTML = `<tr class="js-table-header">
<td>Naam:</td>
<td>Toevoegdatum:</td>
<td>Vervaldatum:</td>
<td>Aantal:</td>
</tr>`;
for (let object of data) {
const amount = object.amount;
const name = object.name;
const addDate = object.date;
const exDate = object.expirationDate;
table.innerHTML += `<tr class="js-tr">
<td>${name}</td>
<td>${addDate}</td>
<td>${exDate}</td>
<td>${amount}</td>
</tr>`;
}
listenToTrash();
};
Here I try to add a cell with an SVG
const listenToTrash = function() {
const trash = document.querySelector('.js-trash');
trash.addEventListener('click', function() {
const tableHeader = document.querySelector('.js-table-header');
tableHeader.innerHTML = `<tr class="js-table-header">
<td>Naam:</td>
<td>Toevoegdatum:</td>
<td>Vervaldatum:</td>
<td>Aantal:</td>
<td>Verwijderen:</td>
</tr>`;
const tableRow = document.querySelectorAll('.js-tr');
console.log(tableRow)
for (row of tableRow){
tableRow.innerHTML +=
'<td> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M3 8v16h18v-16h-18zm5 12c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.552.448-1 1-1s1 .448 1 1v8zm5 0c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.552.448-1 1-1s1 .448 1 1v8zm5 0c0 .552-.448 1-1 1s-1-.448-1-1v-8c0-.552.448-1 1-1s1 .448 1 1v8zm4-15.375l-.409 1.958-19.591-4.099.409-1.958 5.528 1.099c.881.185 1.82-.742 2.004-1.625l5.204 1.086c-.184.882.307 2.107 1.189 2.291l5.666 1.248z"/></svg></td>';
}
});
};
<td>in an array and then do ajoin, and add to theinnerHTML, to add a new one just push it to the array