Javascript: How To Iterate Through A Table And Remove The Second Cell In Javascript
Lets say that I have a very simple table:
How
TextFirst | TextSecond |
Solution 1:
In your posted example you have, at least, two options I can think of:
$('td:nth-child(2)').remove();
Or:
$('tr td:eq(1)').remove(); // zero-based index, thanks @ZDYN for the catch
Or, in plain JavaScript:
var removeThis = document.getElementsByTagName('td')[1];
removeThis.parentNode.removeChild(removeThis);
Solution 2:
$("table td:nth-child(2)").remove()
Solution 3:
//var table = document.getElementById("?");
for (i=0; i < table.rows.length; i++)
{
if (table.rows[i].cells.length > 1)
table.rows[i].cells[1].removeNode(true);
}
or if you want to delete cells based on some condition. just because
//var table = document.getElementById("?");
for (i=0; i < table.rows.length; i++)
{
for (j = table.rows[i].cells.length - 1; j >= 0; j--)
{
if (table.rows[i].cells[j].innerHTML == 'TextSecond')
table.rows[i].cells[j].removeNode(true);
}
}
Post a Comment for "Javascript: How To Iterate Through A Table And Remove The Second Cell In Javascript"