jQuery addClass example

Change class name on click in jQuery

by Hoang_Van_Trinh

HTML

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

<div class="container">
	<div class="row">
      <div class="mouse_1 col-md-4" style="background-color: red">Move the mouse pointer over this paragraph.</div>
      <div class="mouse_2 col-md-4" style="background-color: yellow">Move the mouse pointer over this paragraph.</div>
      <div class="mouse_3 col-md-4" style="background-color: black">Move the mouse pointer over this paragraph.</div>
     </div>
</div>

</body>

CSS

body {
  background: white;
  padding: 20px;
  font-family: Helvetica;
};

JavaScript

$(document).ready(function(){
  $(".mouse_2").hide();
  $(".mouse_3").hide();
  $(".mouse_1").mouseenter(function(){
    $(".mouse_2").show();
  });
  $(".mouse_2").mouseenter(function(){
    $(".mouse_3").show();
  });
  $(".mouse_2").mouseleave(function(){
    if($('.mouse_3')){
         $(".mouse_3").show();
     } else {
          $(".mouse_2").hide();
          $(".mouse_3").hide();
    }
  });
  
    $(".mouse_1").mouseleave(function(){
    if($('.mouse_2').is(':hover')){
    	//alert("Hello Trinh");
         $(".mouse_3").show();
     } else {
          $(".mouse_2").hide();
          $(".mouse_3").hide();
    }
  });
 
   $(".mouse_3").mouseleave(function(){
   	  $(".mouse_3").hide();
      if($('.mouse_2').is(':hover')){
        $(".mouse_3").show();
       } else if ($('.mouse_1').is(':hover')) {
       $(".mouse_2").show();
      } else {
          $(".mouse_2").hide();
          $(".mouse_3").hide();
    }
  });
});