How to change the color to an html cell from a JS

1

I am new to this programming, I need your help with a question, I have this html:

<td>{{data.macEntra}}</td>
<td>{{data.macSale}}</td>
<td>{{data.paquetes}}</td>
<td>{{data.perfil}}</td>
<td>{{data.ciudad}}</td>
<td>{{data.proceso}}</td>
<td>{{data.accion}}</td>
<td id="prueba">{{data.horagestion}}</td>   
<td>{{data.producto}}</td>
<td>{{data.tipoEquipo}}</td>
<td>{{data.remite}}</td>

And I have another file in JS in which there is a validation of time and when a condition is met I need to change the color to the background of <td id="prueba">{{data.horagestion}}</td> but I do not know how to do it.

This is the code in the JS:

            TV.forEach( function(valor, indice) 
        {
             var diferencia=new Date(js_yyyy_mm_dd_hh_mm_ss())-new Date(TV[indice]);


            if (diferencia > 900000) 
            {
                console.log("Alerta!");

            }
        });

Someone who can guide me would appreciate it.

Best regards

    
asked by Hector Echeverri 26.09.2018 в 16:24
source

2 answers

1

To change the color you have a condition, you can use javascript. In my case, it changes when I press a button, but you can do the same in your validation. The sentence that changes it is:

  document.getElementById('prueba').style.backgroundColor = "blue";

  function cambiarColor(){
    document.getElementById('prueba').style.backgroundColor = "blue";
  }
td{border:solid 1px;}
<table>
  <tr>
    <td>{{data.accion}}</td>
    <td id="prueba">{{data.horagestion}}</td>   
    <td>{{data.producto}}</td>
  </tr>
</table>

<button type="button" onclick="cambiarColor();">Cambiar color</button>
    
answered by 26.09.2018 / 16:35
source
0

The first thing you should do is capture the input that you want to change color, you can assign a class to the input, like this:

var campo=document.getElementsByClassName("color");

Then you assign it the color like this;

campo.style.backgroundColor="blue";
    
answered by 26.09.2018 в 16:38