It’s basically like the friend said in the other answer, but you can make it into shape using querySelector(".tipoMov")
. When going through each row of the table, querySelector will fetch the element with the class .tipoMov
and take the text inside it and compare it to Receita
. If equal, apply the background color:
const linha = document.getElementsByClassName("linha");
for(let i of linha){
if(i.querySelector(".tipoMov").textContent.trim() == "Receita"){
i.style.backgroundColor = "#90EE90";
}
}
<table border="1">
<tr class="linha">
<td class="tipoMov">Receita</td>
<td>Corte de cabelo</td>
<td>R$20,00</td>
</tr>
<tr class="linha">
<td class="tipoMov">Outra coisa</td>
<td>Corte de cabelo</td>
<td>R$20,00</td>
</tr>
<tr class="linha">
<td class="tipoMov">Receita</td>
<td>Corte de cabelo</td>
<td>R$20,00</td>
</tr>
</table>
Note that I used .trim()
to eliminate possible spaces in the borders of the text. This is because if you indent the text inside the td
, the text will return with spaces before and after it.
For example, if you do so:
<td class="tipoMov">
Receita
</td>
The word Receita
will come with spaces, because HTML considers new lines as a white space.
In case the element I want to change the background is the class "line", so the background that was changed was the class "typeMov"
– Luc