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.
cellIndexisn't what you expect in more complex tables