Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

mouseover command

KINDLY HELP MY .THE IMAGES WITH ID #up and #down are flickering on mouseover.. please help anyone

HTML FILE: <input type = "text" id ="quantity">

<img src ="up.png" id ="up"> <img src = "down.png" id ="down"> </input> CSS FILE:

quantity{

width:90px;
position:absolute;
margin-top:310px;
right:490px;
height:50px;

}

up{

height:20px;
width:20px;
margin-top:310px;
position:absolute;
right:500px;
display:none;

}

down{

height:20px;
width:20px;
margin-top:340px;
position:absolute;
right:500px;
display:none;

} JAVASCRIPT FILE: let quantity = document.getElementById('quantity'); let up = document.getElementById('up'); let down = document.getElementById('down'); quantity.addEventListener('mouseover',()=>{ up.style.display="block"; up.style.cursor = "pointer"; down.style.display="block"; down.style.cursor="pointer"; }); quantity.addEventListener('mouseout',()=>{ up.style.display="none"; down.style.display="none"; });

2 Answers

Unable to replicate the issue on JSFiddle... https://jsfiddle.net/zcd3jubg/3/

Steven Parker
Steven Parker
243,266 Points

It looks like you've created a "visual loop" with your event handlers and positioning. By placing the images over the input, when they appear under the pointer that will cause a mouseout event for the input element. Then when they are removed, a mouseover event will occur for the input again. You can demonstrate this by keeping the pointer away from the images as you move it in and out of the input area and you won't have the flickering.

An easy fix would be to position your image elements so they don't overlap the input element. Otherwise, you might need additional handlers (or a delegated handler) and code to check if the pointer is still within the bounds of the parent element before doing anything to the images.

In future postings, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.