Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.37 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to simplify multiple mouseover and mouseout functions? [migrated]
  2. $('.remove').eq(0).mouseover(function(){
  3.     $('.tooltip').eq(0).show();
  4. });
  5.  
  6. $('.remove').eq(0).mouseout(function(){
  7.     $('.tooltip').eq(0).hide();
  8. });
  9.  
  10. $('.remove').eq(1).mouseover(function(){
  11.     $('.tooltip').eq(1).show();
  12. });
  13.  
  14. $('.remove').eq(1).mouseout(function(){
  15.     $('.tooltip').eq(1).hide();
  16. });
  17.        
  18. var tooltips = $('.tooltip');
  19.  
  20. $('.remove').each(function(i) {
  21.     var tooltip = tooltips.eq(i);
  22.  
  23.     $(this).on({
  24.         mouseover: function() { tooltip.show(); },
  25.         mouseout:  function() { tooltip.hide(); }
  26.     });
  27. });
  28.        
  29. <div id="tooltip1" style="display:none">TOOLTIP 1 CONTENT</div>
  30. <div id="tooltip2" style="display:none">TOOLTIP 2 CONTENT</div>
  31. <div id="tooltip3" style="display:none">TOOLTIP 3 CONTENT</div>
  32.  
  33.  
  34.  
  35. <a href="javascript:;" class="remove" data-ref="#tooltip1">TRIGGER 1</a>
  36. <a href="javascript:;" class="remove" data-ref="#tooltip2">TRIGGER 2</a>
  37. <a href="javascript:;" class="remove" data-ref="#tooltip3">TRIGGER 3</a>
  38.  
  39. <script type="text/javascript">
  40.  $('.remove').mouseover(function(){
  41.         $($(this).attr("data-ref")).show();
  42.     });
  43.  
  44.  $('.remove').mouseout(function(){
  45.         $($(this).attr("data-ref")).hide();
  46.     });
  47. </script>
  48.        
  49. $('.remove').hover(function(){
  50.     $('.tooltip').eq($(this).index()).show();
  51. }, function(){
  52.     $('.tooltip').eq($(this).index()).hide();
  53. });