javascriptjquerypopuponmouseout

Hide popup when mouse moves off


I'm trying to add a popup when a td is mousedover. Each row has multiple td's and the popup should only work on the first one. This works as long as mouseout is in the same column. That is, if I move the mouse up and down, the popup appears and disappears as expected. But if I move the mouse horizontally into the next td, the popup doesn't disappear. I created a jsfiddle for this but the popup isn't working. The console is saying the javascript function isn't defined but it works fine here so I must have something wrong in the jsfiddle setup. Here's the code I am using. Td's are being used because this is the code I was given. Can anyone see what is needed to get the popup to hide no matter how the mouse moved?

EDITED to solve the problem.

    <style>
    #pop-description{
      display              : none;
      position             : absolute;
      z-index              : 99999;
      left                 : 0px;
      padding              : 10px;
      background           : #3AB9AE;
      border               : 1px solid #9a9a9a;
      margin               : 0px;
    }
    </style>

    <script>
    $(document).ready(function(){
    function ShowDescription(id) { 
     var position = $('.class-desc-'+id).position();
     var desc = $('#desc-'+id).val();
     $('#pop-description').css('top', position.top);
     $('#pop-description').text(desc);
     //$('#pop-description').toggle();

     $('.class-desc-'+id).mouseenter(function() {
       $('#pop-description').css('display', 'block');

     }).mouseleave(function() {
       $('#pop-description').css('display', 'none');
     });  
    }
    });
    </script>

    <div style="display:relative;"><div id="pop-description" style="display:none;"></div></div>
    <table>
    <tr>
    <td class="class-desc-0" onmouseOver="ShowDescription('0');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-0" value="first test" id="desc-0">
    </tr>

    <tr>
    <td class="class-desc-1" onmouseOver="ShowDescription('1');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-1" value="second test" id="desc-1">
    </tr>

    <tr>
    <td class="class-desc-2" onmouseOver="ShowDescription('2');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-2" value="third test" id="desc-2">
    </tr>
    </table>  
      

Solution

  • I think you are overthinking it. Here is what I would do. I would use jQuery as demonstrated below.

    1. Trigger the action you need on mouseenter
    2. Initiate the opposite action on mouseleave

    $(function() {
    
      $(".toggle").mouseenter(function() {
        // Your code goes below: initiate first action
        $(this).addClass("showOff");
    
      }).mouseleave(function() {
    
        // Your code goes below: Initiate opposite action
        $(".toggle").removeClass("showOff");
    
      });
    
    });
    div {
      cursor: pointer;
      width: 200px;
      height: 200px;
      line-height: 200px;
      text-align: center;
      transition: all 2s;
    }
    
    .showOff {
      width: 200px;
      height: 200px;
      line-height: 200px;
      text-align: center;
      background: orange;
      transition: all 2s;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="toggle">Hove over me</div>

    Note: In your case, you show the popup on mouseenter and hide it on mouseleave