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

140
Visualizações
console.log(Element) after change style. But not working I expected. Different result in local chrome and snippet in StackOverflow

I made a simple function which print element in console before change a text color when I click the "Click me".

function addEventToTitle(){
    const title = document.getElementsByClassName("title")[0];
    title.addEventListener('click', changeColorOfTitle);
  }
  
  function changeColorOfTitle(){
    const title = document.getElementsByClassName("title")[0];
    console.log(title);
    title.style.color = "red";
  }
  
  addEventToTitle();
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script defer type="text/javascript" src="test.js"></script>
    <title>Document</title>
</head>
<body>
    <Button class="title">Click me</Button>
</body>
</html>

In snippet, It works like I expected. like below

<button class="title">Click me</button>

But, when I tried this in local. It always prints like this

<button class="title" style="color: red;">Click me</button>

The result should not imply style="color: red; because console.log is executed before change color.

Same code but different result, I don't know what is the reason for this.

about 4 years ago · Juan Pablo Isaza
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