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

255
Views
How to prevent onmouseout function to get called when onclick has been called already?

I'm trying to create a Like button with an input with the type image which its src changes when it is hovered/clicked. I want to change it when it is hovered and have its previous source back when it is unhoverd. So I used onmouseover and onmouseout to do this. I also used onclick to the src when it is clicked on.
The problem is that when I click on it, it changes as intended but as soon as I move the mouse so it is not hovered anymore, It calls the onmouseout function and this is not what I need.

Edit: There are multiple buttons so I cant't use a boolean variables to check if was clicked.
What should I do to not execute onmouseout when it was clicked on?

<input type="image" src="path" onmouseover="likeHover(this)" onmouseout="likeUnhover(this)" onclick="like(this, '{{post.id}}')">
function likeHover(btn) {
    btn.src = '../../static/network/img/heart-pink.png';
}

function likeUnhover(btn) {
    btn.src = '../../static/network/img/heart-black.png';
}

function like(btn) {
    btn.src = '../../static/network/img/heart-red-filled.png';
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use a boolean variable, and set it to true if the button was clicked. And use addEventListener in the JS code and not the HTML attributes:

const input = document.querySelector("input[type=image]");
  let clicked = false;

input.addEventListener("mouseover", (e) => {
  if (!clicked)
    e.target.src = '../../static/network/img/heart-pink.png';
});

input.addEventListener("mouseout", (e) => {
  if (!clicked)
    e.target.src = '../../static/network/img/heart-black.png';
});

input.addEventListener("click", (e) => {
  clicked = true;
  e.target.src = '../../static/network/img/heart-red-filled.png';
});
<input type="image" src="path">

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!