Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!