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

156
Visualizações
How can I cancel changes applied to an html element when hovering ( or clicking) on that element, after the relevant code is runned and finished?

I am trying to understand the DOM manipulation through JS, As an example, I used a form. Let's suppose I hover over an input field or even click, the background of the form will go dark( just an example). How can I cancel this change ( so that the background ocne again becomes light-blue), once I hover out the input field or click outside the form?

here is the code I am using for this : https://codepen.io/abdou-web-dev/pen/QWMxwdB

document.querySelectorAll("input").forEach((inputt) => {
    inputt.addEventListener('click', funct3);

    function funct3(e2) {
        myform.style.backgroundColor = "black"
    };
});

any help will be appreciated, thanks !

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

0

You can use the 'blur' event listener for this.

document.querySelectorAll("input").forEach((inputt) => {
    inputt.addEventListener('blur', funct3);

    function funct3(e2) {
        myform.style.backgroundColor = "lightblue";
    };
});

The blur event occurs when a DOM element loses its focus.

You can even implement this with pure css.

Use the :hover pseudo-class or :focus pseudo-class and you can write your color changes there

about 4 years ago · Juan Pablo Isaza Relatório

0

Thanks to scandav, I found the solution , which is :

document.querySelectorAll("input").forEach((inputt) => {
    inputt.addEventListener('mouseleave', funct3);
    function funct3(e2) {
        myform.style.backgroundColor = "rgba(135, 206, 250, 0.281)"
    };
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Another way to implement this (if you're going for hover effects) would be to use mouseenter and mouseleave events.

Like this...

document.querySelectorAll("input").forEach((inputt) => {
    inputt.addEventListener('mouseenter', funct3);
    inputt.addEventListener('mouseleave', funct4);

    function funct3(e2) {
        myform.style.backgroundColor = "black"
    };
    function funct4(e2) {
        myform.style.backgroundColor = "lightblue"
    };
});
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