.hover()

Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements. Bind a single handler to the matched elements, to be executed when the mouse pointer enters or leaves the elements.

.hover(handlerIn, handlerOut)🡢 jQuery

handlerIn FunctionA function to execute when the mouse pointer enters the element.
handlerOut FunctionA function to execute when the mouse pointer leaves the element.

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

Calling $( selector ).hover( handlerIn, handlerOut ) is shorthand for:

$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

See the discussions for .mouseenter() and .mouseleave() for more details.

To add a special style to list items that are being hovered over, try:

JS
<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li class="fade">Chips</li>
  <li class="fade">Socks</li>
</ul>
CSS
ul {
  margin-left: 20px;
  color: blue;
}
li {
  cursor: default;
}
span {
  color: red;
}
HTML
$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  },
  function () {
    $(this).find("span").last().remove();
  }
);

$("li.fade").hover(function () {
  $(this).fadeOut(100);
  $(this).fadeIn(500);
});
DEMO

To add a special style to table cells that are being hovered over, try:

HTML
$("td").hover(
  function () {
    $(this).addClass("hover");
  },
  function () {
    $(this).removeClass("hover");
  }
);
DEMO

To unbind the above example use:

HTML
$("td").off("mouseenter mouseleave");
DEMO

.hover(handlerInOut)🡢 jQuery

handlerInOut FunctionA function to execute when the mouse pointer enters or leaves the element.

The .hover() method, when passed a single function, will execute that handler for both mouseenter and mouseleave events. This allows the user to use jQuery's various toggle methods within the handler or to respond differently within the handler depending on the event.type.

Calling $(selector).hover(handlerInOut) is shorthand for:

$(selector).on("mouseenter mouseleave", handlerInOut);

See the discussions for .mouseenter() and .mouseleave() for more details.

Slide the next sibling LI up or down on hover, and toggle a class.

JS
<ul>
  <li>Milk</li>
  <li>White</li>
  <li>Carrots</li>
  <li>Orange</li>
  <li>Broccoli</li>
  <li>Green</li>
</ul>
CSS
ul {
  margin-left: 20px;
  color: blue;
}
li {
  cursor: default;
}
li.active {
  background: black;
  color: white;
}
span {
  color: red;
}
HTML
$("li")
  .odd()
  .hide()
  .end()
  .even()
  .hover(function () {
    $(this).toggleClass("active").next().stop(true, true).slideToggle();
  });
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)