3

It can be awkward because English is not my first language.

I have a task to write pure javascript code that deletes the clicked column when the table is clicked. Unfortunately I can't use jquery.

let cells = document.querySelectorAll("table td"); is the code I basically need to use.

window.onload = function () {
      function click(e) {
        let cells = document.querySelectorAll("table td");
        let index = e.target.cellIndex;
        cells.forEach((td, i) => {
          if (td.cellIndex === index) {
            td.parentElement.querySelectorAll("td")[index].remove();
          }
        })
      }
      
      // set onclick event for the head of each column
      document.querySelectorAll('tr > td').forEach(td => td.onclick = click)
    }

In my source, I delete all the columns to the right of where I clicked to see where the problem is.

Only the column of the clicked td should be deleted.

I don't know where the problem is or how to fix it.

2
  • We need to have an idea of the shape of your table to help, somethimes the cellIndex isn't what you expect in more complex tables Commented Dec 12, 2021 at 12:43
  • 1
    It's actually a good thing that you can't use JQuery. Still seeing it in way too much projects. Commented Dec 12, 2021 at 12:51

2 Answers 2

4

You can select the cells you want to remove via the nth-child selector, based on cellIndex. The below table demonstrates this:

function click(e) {
  let index = e.target.cellIndex;
  document.querySelectorAll(`tr td:nth-child(${index + 1})`).forEach(cell => cell.remove());
}

// set onclick event for the head of each column
document.querySelectorAll('tr > td').forEach(td => td.onclick = click)
<table border="1">
  <tr><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td><td>Column 5</td></tr>
  <tr><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td><td>Column 5</td></tr>
  <tr><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td><td>Column 5</td></tr>
  <tr><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td><td>Column 5</td></tr>
  <tr><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td><td>Column 5</td></tr>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

good and short answear! nice
1

Collect all tr. Apply click eventListener to every single tr. The EventHandler collect all td's by column index. Then use remove() function.

document.querySelectorAll('tr').forEach((c) => {
  c.addEventListener('click', (e) => {
    document.querySelectorAll('tr td:nth-child(' + (e.target.cellIndex + 1) + ')')
     .forEach((td) => {
      td.remove();
    });
  })  
});
table {
  border: 1px solid #000;
}
<table>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
  <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td></tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.