Berend Vandenbussche
Berend Vandenbussche

Reputation: 106

Is there any way to add a <td> to each table row using javascript

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>';
    }
  });
};

Upvotes: 2

Views: 1274

Answers (1)

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

Use forEach to loop through your .js-tr, then create an element td set your svg as a child of your td, then append it to each of js-tr.

const tableRow = document.querySelectorAll('.js-tr');
tableRow.forEach(function (el) {
  const td = document.createElement('td');
  td.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>';
  el.appendChild(td);
})

Update: Here the snipcode with onclick event:

const tableRow = document.querySelectorAll('.js-tr');
tableRow.forEach(function (el) {
  const td = document.createElement('td');
  td.className = 'hidden';
  td.style.cursor = 'pointer';
  td.innerHTML = '<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>';
  el.appendChild(td);
  td.onclick = function () {
    if (this.classList.contains('hidden')) {
      this.classList.remove('hidden');
    } else {
      this.classList.add('hidden');
    }
  }
})
.hidden svg {
  opacity: 0;
  visibility: hidden;
}
.hidden {
  background-color: red;
}
<table>
  <tr class="js-tr">
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

Upvotes: 4

Related Questions