jQuery hover: Learn in 10 minutes with a live demo

What is jQuery hover() method?

In modern websites, we usually see when the mouse pointer is moved to links or sometimes in content paragraphs or images etc. the color of link changes or other effects in images or entered paragraphs happen. This can be done in different ways, generally, the hover() method is used for this purpose.

The Hover jQuerymethod attaches or binds one or two event handlers or functions to the specified elements (like links, divs, paragraphs, images). These handlers or function executes as mouse pointer enters or leaves the specified/matched elements.

How to use the hover() method?

Following is the general syntax of using the hover method:

$( element).hover(handlerInFunction, handlerOutFunction)

e.g.

$(“p”).hover(handlerInFunction, handlerOutFunction)

$(“div”).hover(handlerInFunction, handlerOutFunction)

Where

  • handlerInFunction : Required. This function will execute as mouse pointer enters the specified element
  • handlerOutFunction: Optional. This handler or function will execute as mouse leaves the element.

If only one handler is given then it will execute for both the mouseenter and mouseleave events.

Running example of hover() method

In the following example, as the mouse enters the link area, the color of the link changes with the text in the paragraph as well. As the mouse is taken out of the link area the color will again change along with the text.

jQuery hover

Experience this example online

Read also: jQuery mouseenter | jQuery mouseleave