Are you wanting to remove the content from the cell, or the cell itself?
You can’t really remove a cell from a table, it would leave a gap. The only way is to increase the span of an adjacent cell to take its space.
Good point… in any case, it would probably be desirable to get the actual text nodes first; then you can remove their parent elements or do whatever is appropriate. This can be done using a tree walker that only accepts text nodes containing a given string:
// Creates a tree walker that finds text
// nodes containing a given string
function createTextWalker (root, text) {
return document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
acceptNode (node) {
return node.textContent.includes(text)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT
}
})
}
// Iterator wrapper for convenience
function iterateTreeWalker (walker) {
return {
[Symbol.iterator] () {
return this
},
next () {
const value = walker.nextNode()
return { value, done: !value }
}
}
}
// Find the text nodes and remove their
// parent elements
const textNodes = iterateTreeWalker(
createTextWalker(document.body, 'remove')
)
for (const node of textNodes) {
node.parentElement.remove()
}
As I say, a table is a two dimensional grid of a fixed number of rows and columns. You can’t have a different number of columns in a particualr row, except by increasing the span of another cell to knock the cell out.
So can we assume you want to add a colspan or rowspan attribute to an adjacent cell to the one in question? Because nothing else makes sense, except if it were only a single row (one dimensional) table, but semantically, that’s not really a table, that’s a list.