jQuery .hover() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Mouse Events

Definition

.hover() binds handlers for mouseenter and mouseleave in one call. With two functions it runs enter then leave; with one function (since 1.4) the same handler runs for both. Prefer .on in new code.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#box").hover(fnIn, fnOut)   // deprecated
$("#box").hover(fnInOut)       // deprecated (1.4+)

// Modern:
$("#box").on("mouseenter", fnIn).on("mouseleave", fnOut)
$("#box").on("mouseenter mouseleave", fnInOut)
CallWhat it does
handlerInRuns on mouseenter
handlerOutRuns on mouseleave
handlerInOutRuns on enter and leave
Return valuejQuery set (chainable)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select element(s): $("li"), $("#box"), etc.
Step 2Two handlers (legacy): .hover(fnIn, fnOut).
Step 3One handler (legacy, 1.4+): .hover(fn) — check event.type if needed.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — .hover(fnIn, fnOut)

Official style: enter appends a marker; leave removes it.

jQuery
$("li").hover(
  function () {
    $(this).append(" ***");
  },
  function () {
    $(this).text($(this).text().replace(" ***", ""));
  }
);
Try It Yourself

How It Works

  1. fnIn runs on mouseenter — appends ***.
  2. fnOut runs on mouseleave — removes the marker.
  3. Same as separate mouseenter / mouseleave binds.

Example 2 — Mark a Cell on Enter

Official “highlight” idea, CSS-free: prefix text while hovered.

jQuery
$("td").hover(
  function () {
    $(this).data("label", $(this).text());
    $(this).text("[*] " + $(this).data("label"));
  },
  function () {
    $(this).text($(this).data("label"));
  }
);
Try It Yourself

How It Works

  1. Enter stores the original text, then prefixes [*].
  2. Leave restores the saved label.
  3. No CSS required — pure text feedback.

Example 3 — Unbind with .off()

Official cleanup: remove enter/leave handlers by those event names.

jQuery
$("td").hover(fnIn, fnOut);

$("#remove").on("click", function () {
  $("td").off("mouseenter mouseleave");
});
Try It Yourself

How It Works

  1. .hover() registers under mouseenter and mouseleave.
  2. There is no separate "hover" event name.
  3. .off("mouseenter mouseleave") removes both.

Frequently Asked Questions

With two functions — .hover(handlerIn, handlerOut) — it binds mouseenter and mouseleave. With one function since 1.4 — .hover(handlerInOut) — the same function runs for both events. Both forms are deprecated since jQuery 3.3.
Yes. Prefer .on("mouseenter", handlerIn).on("mouseleave", handlerOut) for two handlers, or .on("mouseenter mouseleave", handlerInOut) for one. jQuery 3.x still runs .hover() for compatibility.
CSS :hover is stylesheet-only. .hover() runs JavaScript on enter/leave — useful for DOM changes and logic CSS cannot express. Prefer CSS for pure styling.
.hover() uses mouseenter/mouseleave, which do not re-fire when moving over child elements. mouseover bubbles and re-fires on child boundaries — often noisier for menus and highlights.
Use .off("mouseenter mouseleave") on the same elements. There is no separate "hover" event name — .hover() registers under mouseenter and mouseleave.
Replace .hover(fnIn, fnOut) with .on("mouseenter", fnIn).on("mouseleave", fnOut). Replace .hover(fn) with .on("mouseenter mouseleave", fn).

Next: mousedown Event — Handle Mouse Button Press

Fire when the mouse button is pressed — before release.

mousedown Event — Handle Mouse Button Press tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful