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

134
Visualizações
How can i reset my document.getElementById color value

I'm trying to change the color from red to blue but when the red color come its unchangeable

var colorvalue=0;
var sizevalue;
function CG()
{
    colorvalue ++;
    if (colorvalue==0)
    {
        document.getElementById("testd").style.color = "blue";
    }
    else if(colorvalue==1)
    {
        document.getElementById("testd").style.color = "red";
    }
    if(colorvalue > 1)
    {
        colorvalue = 0;
    }
}

I already got the answer but how do i have more colors

Thanks for seeing my question and if you do thanks for answering

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

i edit a bit of your code. try below

var colorvalue=0;
var sizevalue;
function CG()
{
    colorvalue ++;
    if (colorvalue % 2 == 0)
    {
        document.getElementById("testd").style.color = "blue";
    }
    else
    {
        document.getElementById("testd").style.color = "red";
    }
}

Used modulo operation: So if there is a remainder of colorvalue, element will be red.

about 4 years ago · Juan Pablo Isaza Relatório

0

An alternative method would be to use CSS rather than setting the style directly on the element using JavaScript.

Initialise your text with the color red, and then toggle a blue class on/off using classList.

const test = document.querySelector('.test');
const button = document.querySelector('button');
button.addEventListener('click', handleClick, false);

function handleClick() {
  test.classList.toggle('blue');
}
.test { color: red; }
.blue { color: blue; }
<p class="test">This is a test</p>
<button>Change color</button>

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