• 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
  • VBA
  • About
You are here: Home / jQuery / jQuery mouseover – An Interactive Example of the mouseover Method

jQuery mouseover – An Interactive Example of the mouseover Method

January 29, 2022 Leave a Comment

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

$("#div1").mouseover(function() {
  //The user has moved their mouse into #div1
});

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

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

The jQuery mouseover() method is very similar to the mouseenter() method, the main difference being that the mouseover method will trigger when you mouse over any child elements of the div, while the mouseenter method will not. We will show this in the example below.

An example using the jQuery mouseover method

In this example, we will have 3 divs set up next to each other. One div will be empty, and the other two will have a child element that takes up half of the div. We will add some CSS to float the divs next to each other and display the text in the center.

Here is the HTML setup:

<style>#div1, #div2, #div3 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; } #child1, #child2 { height: 100px; width: 100%; background: #ddd; display: flex; align-items: center; justify-content: center; padding: 5px; } .clear { clear: both; }</style>
<div id="div1">div1</div>
<div id="div2">
  <div id="child1">mouseover method</div>
</div>
<div id="div3">
  <div id="child2">mouseenter method</div>
</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 third div, we will use the mouseenter method to show how it differs from the mouseover method.

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").mouseover(function() {
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseover(function() {
  $('#div2').css("background-color", genRandomColor());
});
$("#div3").mouseenter(function() {
  $('#div3').css("background-color", genRandomColor());
});

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

Code Output:

div1
mouseover method
mouseenter method

Full Code:

<style>#div1, #div2, #div3 { float: left; height: 200px; width: 200px; border: 1px solid #444; margin-right: 20px; } #child1, #child2 { height: 100px; width: 100%; background: #ddd; display: flex; align-items: center; justify-content: center; padding: 5px; } .clear { clear: both; }</style>
<div id="div1">div1</div>
<div id="div2">
  <div id="child1">mouseover method</div>
</div>
<div id="div3">
  <div id="child2">mouseenter method</div>
</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").mouseover(function() {
  $('#div1').css("background-color", genRandomColor());
});
$("#div2").mouseover(function() {
  $('#div2').css("background-color", genRandomColor());
});
$("#div3").mouseenter(function() {
  $('#div3').css("background-color", genRandomColor());
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Detect Window Resize
  • 2.  How to Use jQuery to Check if an Element is Visible
  • 3.  Using jQuery to Get the Parent Div of an Element
  • 4.  Using jQuery to Change the Id of a Div
  • 5.  Using jQuery to Set Visibility
  • 6.  jQuery val Method – Get the Value from an Input Field
  • 7.  Using jQuery to Get Value of Select On Change
  • 8.  How to Set All Checkbox to Checked in jQuery
  • 9.  Using jQuery to see if URL Contains Specific Text
  • 10.  Using jQuery to Get the Bottom Position of 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy