The background color is getting applied to only c1 class and not c2 even though the code is same. When i inspect it I am getting
Uncaught TypeError: Cannot set property 'backgroundColor' of undefined
for the first class itself. I can’t find anything wrong with the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
table,th,td{
border: 1px solid black;
border-collapse: collapse;
}
</style>
<table style="width:75%" >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr class='c1'>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class='c2'>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr class='c1'>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr class='c2'>
<td>Greg</td>
<td>House</td>
<td>50</td>
</tr>
</table>
<script>
var c1=document.getElementsByClassName('c1');
console.log(c1)
for(i in c1)
{
c1[i].style.backgroundColor='aqua';
}
var c2=document.getElementsByClassName('c2');
for(var j in c2)
{
c2[j].style.backgroundColor='coral';
}
</script>
</body>
</html>