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

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

0

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

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 Report

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 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!