How to modify the color of a tr according to the data of a td?

3

I own some tr in the table that contains the tr-child class and would like to change their color when the value of the fourth column of that tr is> 0. Is this possible with jquery?

I had already done something ...

$('.tr-child td:nth-child(4)').each(function(index, element){
if(element.innerHTML > 0){
// precisaria setar a tr referente a esta td aqui <--
}
});
    
asked by anonymous 11.02.2016 / 12:33

2 answers

2

See the example using the find ()

  

Tip: Instead of using innerHTML of JavaScript, use html() of jQuery, you may have problems with some browsers   using innerHTML and html() will instantiate the function of the   JavaScript after some checks.

$('table tr').each(function() {
  var valor = parseInt($(this).find('td:nth-child(3)').html());
  if (valor > 60)
    $(this).addClass('colorido');
});
.colorido {
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tablestyle="width:100%">
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>
    
11.02.2016 / 12:50
0

After creating the question I did a quick search and was able to solve the problem using jquery's closest () method, thus:

$('.tr-child td:nth-child(4)').each(function(index, element){
  if(element.innerHTML > 0){
    $(element).closest('tr').attr('id','colorir');
  }
});
#colorir td{
  color:green;
}
    
11.02.2016 / 12:43