I have 2 div tag. The first contains a table. The second contains some text. When the page is loaded both div have no background-color. When there is an hover on a td of the first div, the second div background-color is set to yellow (see code below). My problem is that when I leave the table with the mouse, the background of the second div stays yellow. I would like the color to be remove on leaving the hover state. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.
My HTML :
<div id="div-one">
<table>
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td></tr>
</table>
</div>
<div id="div-two">
some text
</div>
My JS:
$(document).on("hover", "#div-one td", function() {
$("#div-two").css("background-color":"yellow");
});
Is hover deprecated in jQuery? Deprecated in jQuery 1.8, removed in 1.9: The name “hover” used as a shorthand for the string “mouseenter mouseleave” .
The mouseover event is sent to an element when the mouse pointer enters the element. Any HTML element can receive this event.
jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.
Use mouseenter
and mouseleave
events separately - which is actually a "hover" event:
$(document).on({
mouseenter: function() {
$("#div-two").css("background-color", "yellow");
},
mouseleave: function() {
$("#div-two").css("background-color", "");
}
}, "#div-one td");
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With