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

188
Visualizações
When you leave input field, a function is triggered which change input background color

How can I do like, when my cursor leave input field, a function is triggered which change input background color. I was trying to do like this, but did not work well.

<!DOCTYPE html>
<html>
<body>

Enter your name: <input type="text" id="name" onchange="changeColor()">

<script>
function changeColor() {
  const x = document.getElementById("name");
  x.style.background = "red";
}
</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

function changeColor(ev) {
  ev.target.style.backgroundColor = "red";
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the blur event to target specifically the lose of focus:

Enter your name: <input type="text" id="name">

<script>
const x = document.getElementById("name");

x.onblur = changeColor;

function changeColor() {
  x.style.background = "red";
}
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

Try the onmouseleave event listener. onchange usually involved the value of the input field changing.

In CSS you have :focus selector to style based of if the input is focused or not

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