Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
Difference between hover() and mouseover() methods in jQuery
Next article icon

Difference between hover() and mouseover() methods in jQuery

Last Updated : 01 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 exitThis method can fire multiple times in the case of the nested elements.

Next Article
Difference between hover() and mouseover() methods in jQuery

H

hritikrommie
Improve
Article Tags :
  • Web Technologies
  • HTML
  • JQuery
  • jQuery-Methods
  • HTML-Questions
  • jQuery-Questions

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences