jquery hover

What is jQuery hover?

jQuery hover is a method in jQuery library that is used to attach two event handlers to an element, mouseenter, and mouseleave. The mouseenter event handler is executed when the mouse pointer enters the element, and the mouseleave event handler is executed when the mouse pointer leaves the element. It can be used to add effects to an element when a user hovers over it.

Example:


$(document).ready(function(){
  $("p").hover(function(){
    $(this).css("background-color", "yellow");
  }, function(){
    $(this).css("background-color", "white");
  });
});

In the example above, when a user hovers over a <p> element, the background color of the element changes to yellow, and when the user moves the mouse pointer out of the element, the background color of the element changes back to white.

Alternative way:


$(document).ready(function(){
  $("p").mouseenter(function(){
    $(this).css("background-color", "yellow");
  });
  $("p").mouseleave(function(){
    $(this).css("background-color", "white");
  });
});

The above example achieves the same result as the previous example by using separate mouseenter and mouseleave event handlers instead of hover.

jQuery hover can be used to create various effects, such as showing or hiding elements, changing the color or size of an element, or adding animations.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe