How to paint a cell according to the value of a select?

Posted on

Question :

I’m needing help formatting a <td> according to the result of select PHP. I thought about using jQuery but I do not know much about it.

<tr class="">
    <td class=""><?php echo ($row['Id_acordo']); ?></td>
    <td class=""><?php echo ($row['Contrato']); ?></td>
    <td class=""><?php echo ($row['Carteira']); ?></td>
    <td class=""><?php echo ($row['DataPgto']); ?></td>
    <td class=""><?php echo ($row['Valor']); ?></td>
    <td class=""><?php echo ($row['AvistaOuParcelado']); ?></td>
    <td class=""><?php echo ($row['QuitacaoOuAtualizacao']); ?></td>
    <td class=""><?php echo ($row['FormaEnvio']); ?></td>
    <td class=""><?php echo ($row['Recuperador']); ?></td>
    <td class=""><?php echo ($row['Observacao']); ?></td>
    <td class=""><?php echo ($row['Cadastrado']); ?></td>
</tr>

The <td> tags I need to change class according to the condition:

<?php if($row['Cadastrado'] == '0'){ ?>
    <script>$('td').addClass("success");</script>
<?php }else{ ?>
    <script>
       $('td').removeClass("success");
       $('td').addClass("danger");
    </script>   
<?php   }   ?>

Only he or she paints everything by adding the two classes in the <td> tag or adds none. Example below:

<td class="success danger">16</td>

I have tried instead of if else put two loops of if , but it was not.

If possible, put jQuery only without integration with <?php> , as I would like it to auto update with the $(document).ready function.

    

Answer :

Try to use the bg-success class:

Source: W3School

    

Hello, I believe that this is … Replace the <td> you want to paint for this:

<td class="<?php echo ($row['Cadastrado'] == 0 ? 'success' : 'danger' ); ?>">

    

Leave a Reply

Your email address will not be published. Required fields are marked *