• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery hover – An Interactive Example of the hover Method

jQuery hover – An Interactive Example of the hover Method

January 30, 2022 Leave a Comment

We can use the jQuery hover method to run a function when a user moves their mouse over a div and out of it. To do this we can add a function call to the hover() method.

$("#div1").hover(function() {
  //The user has moved their mouse into #div1
}, function(){
  //The user has moved their mouse out of #div1
});

Another way this can be done is to use the on() method:

$("#div1").on("hover", function() {
  //The user has moved their mouse into #div1
}, function(){
  //The user has moved their mouse out of #div1
});

The jQuery hover() method will make use of the jQuery mouseenter and mouseleave methods. We can attach the function call as seen above and do something different when the user enters the div and/or leaves it.

An example using the jQuery hover method

In this example, we will have 3 divs set up next to each other. All 3 divs will be empty. We will add some CSS to float the divs next to each other and center the text.

Here is the HTML setup:

<style>#div1, #div2, #div3 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; text-align: center }</style>
<div id="div1">
  <p>Hover Method</p>
</div>
<div id="div2">
  <p>Mouseenter Method</p>
</div>
<div id="div3">
  <p>Mouseleave Method</p>
</div>
<div class="clear"></div>

When the user moves their mouse into each div, we will generate a random color and then change the background color of the div.

For the first div, we will use the hover method on it. For the second div, we will use the mouseenter method, and for the third div, we will use the mouseleave method to show how they all work.

Here is the JavaScript code:

function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
}
$("#div1").hover(function() {
  $('#div1').css("background-color", genRandomColor());
}, function(){
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseenter(function() {
  $('#div2').css("background-color", genRandomColor());
});
$("#div3").mouseleave(function() {
  $('#div3').css("background-color", genRandomColor());
});

The final code and output for this example of using the jQuery hover() method is below:

Code Output:

Hover Method

Mouseenter Method

Mouseleave Method

Full Code:

<style>#div1, #div2, #div3 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; text-align: center }</style>
<div id="div1">
  <p>Hover Method</p>
</div>
<div id="div2">
  <p>Mouseenter Method</p>
</div>
<div id="div3">
  <p>Mouseleave Method</p>
</div>
<div class="clear"></div>

<script>

function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
}
$("#div1").hover(function() {
  $('#div1').css("background-color", genRandomColor());
}, function(){
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseenter(function() {
  $('#div2').css("background-color", genRandomColor());
});
$("#div3").mouseleave(function() {
  $('#div3').css("background-color", genRandomColor());
});

</script>

Hopefully this article has been useful to help you understand how to use the jQuery hover() method.

Other Articles You'll Also Like:

  • 1.  Using jQuery to see if URL Contains Specific Text
  • 2.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 3.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 4.  Using jQuery to Get the Bottom Position of Element
  • 5.  Using jQuery to Convert a String to Uppercase
  • 6.  Using jQuery to Get the Previous Sibling of an Element
  • 7.  Using jQuery to Get the Position of Element
  • 8.  Using jQuery to Change Background Color
  • 9.  jQuery insertBefore – Insert Element Before Another Element
  • 10.  Using jQuery to Get the Parent Div of an Element

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy