I have to remove certain tr which have a particular class in a table..I tried the below stuff but it does not seem to work
var d1 = document.getElementsByClassName("t1");
for (var i = 0; i < d.length; i++) {
d1[i].parentElement.removeChild(d1[i]);
}
Can someone please guide me in the right direction
<html>
<head>
<script type="text/javascript">
function removeTR (){
}
</script>
</head>
<body onload="javascript:removeTR()">
<table>
<tr >
<td></td>
</tr>
<tr class="t1">
</tr>
<tr class="t1">
</tr>
</table>
</body>
</html>
You have a few typo and another more fundamental problem : you can’t iterate this way and remove at the same time on a dynamic collection (a live NodeList) as returned by getElementsByTagName.
At second iteration you would try to remove element at index 1 but there still would be an element at index 0 which thus would never be removed.
A solution is this :
Demonstration