Difference between hover() and mouseover() methods in jQuery
Last Updated : 01 Sep, 2021
Before learning the difference between the hover() and mouseover() method of jQuery, let's briefly see both methods.
hover() Method: When we hover our mouse cursor to any element, two events happen i.e. mouseenter and mouseleave.
- mouseenter: When we bring the cursor over the element.
- mouseleave: When we remove the cursor from the element.
The hover()method binds handlers for both mouseenter and mouseleave events. Basically, with the hover() method, we will specify what to do when the cursor enters the element and we will specify what to do when the cursor leaves that element.
Syntax:
$( selector ).hover( handlerIn, handlerOut )
Parameters: It accepts two functions i.e. handlerIn and handlerOut.
- handlerIn: This function will be executed when the cursor enters the element.
- handlerOut: (Optional) This function is executed when the cursor leaves the element.
When we provide only one function as an argument to the hover() method, then that function will be executed for both mouseenter as well as mouseleave events.
Example: In this example, we will see how to use the hover() method. We have a word, and we will try to change the color of it whenever the cursor enters the element. The color will change back when the cursor leaves that element.
HTML <!DOCTYPE html> <html> <head> <!-- jQuery library --> <script src= "https://code.jquery.com/jquery-git.js"> </script> </head> <body> <h2>GeeksforGeeks</h2> <script> // Calling hover() method // on h1 tag $("h2").hover( // mouse-enter event function () { // changing the color $("h2").css('color', 'green') }, // mouse-leave event function () { // Putting the color back $("h2").css('color', 'black') }) </script> </body> </html>
Output:
hover method Mouseover() Method: The mouseover() method will be executed when the mouseover event occurs. The mouseover event occurs when the cursor enters the element and then the mouseover() method for that element will be executed. We can also attach a function that will be executed when the mouseover() method is called.
Syntax:
$(selector).mouseover(handler)
Parameter: (Optional) It accepts a function that will be executed when the mouseover() method is called.
Example:In this example, we will see how to use the mouseover() method.
HTML <!DOCTYPE html> <html> <head> <!-- jQuery library --> <script src= "https://code.jquery.com/jquery-git.js"> </script> </head> <body> <h2>GeeksforGeeks</h2> <script> $("h2").mouseover( function () { // changing the color $("h2").css('color', 'red') }) </script> </body> </html>
Output:
Difference between hover() and mouseover() methods:
hover() | mouseover() |
---|
Bind two handlers to the matched elements, to be executed when the cursor enters and leaves the elements. | Bind one handler to the matched elements, to be executed when the cursor enters the elements. |
It accepts a maximum of two functions as arguments, one for the mouseenter and one for the mouseleave event. | It accepts a maximum of one function as an argument which will be executed when a mouseover event occurs. |
In the hover() method, when the cursor enters the element or its child element, the handlerIn function is called once and when the cursor leaves the element, the handlerOut function is called once. | In the mouseover() method, it works the same as in the hover() method, but in the case of nested elements, When the cursor enters the outer part on which the mouseover event is attached, the mouseover() method gets called, but when the cursor enters the inner part, the mouseover() method calls again. |
The handlerIn and handlerOut function will be called once per element per entry and exit | This method can fire multiple times in the case of the nested elements. |
Similar Reads
Difference between $(this) and 'this' in jQuery In this article, we will learn the difference between this and $(this) in jQuery. this keyword: In JavaScript, this keyword is used to refer to the object it belongs to. The value that this stores is the current execution context of the JavaScript program.Thus, when used inside a function this value
3 min read
Explain the differences between empty() remove() and detach() methods in jQuery If you are building something and want to remove some HTML elements on click, hover, or on any event then you should know that jQuery can do it easily for us. There are three jQuery methods that help us to remove HTML elements with some slight differences. These methods are:remove() Method: It compl
3 min read
What's the difference between selector and filter() in jQuery? jQuery Selectors: allow us to select and manipulate HTML element(s). It is used to select HTML elements such as ids, classes, types, attributes, etc using jQuery selectors and then add any CSS properties or events to the selected HTML elements. Syntax: For selecting a button tag the syntax would be
2 min read
Difference between on() and live() or bind() in jQuery jQuery offers various event handlers like on(), live() and bind(). Though, there are some minor differences which are discussed below. bind() method: This method only attaches events to elements which exist beforehand i.e. state of initialized document before the events are attached. If the selector
3 min read
Difference between mouseover, mouseenter and mousemove events in JavaScript Events in JavaScript provide a dynamic interface to the webpage. There are wide variety of events such as user clicking, moving the mouse over an element, etc. Events that occur when the mouse interacts with the HTML document falls under the category of MouseEvent property. mouseover: The onmouseove
2 min read