Notice how if you onmousedown
over the div, it turns from green to red. This is expected. But if you hold the mouse down and drag your cursor off and away from the div, then let go of your mouse, onmouseup
won't function because the cursor is no longer hovering over the div. The div remains red instead of returning to its initial green state.
Should I use onmousemove
?
Any ideas how to solve this?
function mouseDown() {
document.getElementById("test").style.backgroundColor = "red";
}
function mouseUp() {
document.getElementById("test").style.backgroundColor = "green";
}
#test {
position: absolute;
height: 50px;
width: 100px;
background-color: green;
}
<div id="test" onmousedown="mouseDown()" onmouseup="mouseUp()">
</div>