Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
Ajax - change text color based on return value

I am trying to display a sensor value using html/ajax. This works as expected so far, however I want the text color to change according to the value. for example

if value < 50 then fontcolor= blue 
if value >49 then fontcolor = red

is that at all possible?

<p style="color:blue; position: absolute; top: 810px; width: 100px; padding-left: 340px;" id="ofen_VL">0</p>
<script>
setInterval(function() {
  // Call a function repetatively with 10 Second interval
getData();
}, 10000); //10sec

function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ofen_VL").innerHTML =
  this.responseText;
}
 };
xhttp.open("GET", "ofen_VL.txt", true);
xhttp.send();
}

</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add to the response your condition. Something like:

if (this.readyState == 4 && this.status == 200) {
  document.getElementById("ofen_VL").innerHTML = this.responseText;
  if (Number(this.responseText) > 50) {
    document.getElementById("ofen_VL").setAttribute('style', 'color: blue;');
  }
  else {
    document.getElementById("ofen_VL").setAttribute('style', 'color: red;');
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda