Languages
[Edit]
EN

jQuery - mouse enter and mouse leave events example

2 points
Created by:
Root-ssh
6451

Using jQuery it is possible to create mouse enter (mouseenter) and mouse leave (mouseleave) events in following ways.

1. mouseenter and mouseleave methods example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <div id="element" style="padding: 10px; background: orange;">
    Move mouse over element...
  </div>
  <script>

    $(document).ready(function() {
		var element = $('#element');
      
      	element.mouseenter(function() {
        	element.text('Mose over element...');
        });
      
      	element.mouseleave(function() {
        	element.text('Mose left element...');
        });
    });

  </script>
</body>
</html>

2. on method with event name example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <div id="element" style="padding: 10px; background: orange;">
    Move mouse over element...
  </div>
  <script>

    $(document).ready(function() {
		var element = $('#element');
      
      	element.on('mouseenter', function() {
        	element.text('Mose over element...');
        });
      
      	element.on('mouseleave', function() {
        	element.text('Mose left element...');
        });
    });

  </script>
</body>
</html>

3. on method with event object example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <div id="element" style="padding: 10px; background: orange;">
    Move mouse over element...
  </div>
  <script>

    $(document).ready(function() {
		var element = $('#element');
      
      	element.on({
            'mouseenter': function() {
        	    element.text('Mose over element...');
            },
          	'mouseleave': function() {
                element.text('Mose left element...');
            }
        });
    });

  </script>
</body>
</html>
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.