Why, when you hover only on the first item, is the close icon displayed? Where is the mistake?

trooble.ru/test.php

<script> $(document).ready(function(e) { $('#msg').hover(function(e) { $('#msg a').show(); }, function(e) { $('#msg a').hide(); }); $('#msg a').click(function(e) { $('#msg').detach(); }); }); </script> 
  • @dmitriy kiryushin To format the code, select it with the mouse and click on the 101010 editor button. - Nicolas Chabanovsky
  • Thank you, I just haven't figured it out yet - dmitriy kiryushin

3 answers 3

Using the class as a selector, you automatically affect all the elements on the page. Correct the script to:

 $(document).ready(function() { $('.msg').hover(function() { $('a',this).show(); }, function() { $('a',this).hide(); }); $('.msg a').click(function() { $(this).closest('.msg').detach(); }); }); 
  • Thank you so much - dmitriy kiryushin

use the class instead of id, when js searches by id, it finds the element and returns it stopping further searches

  • With this figured out, now see trooble.ru/test.php when selecting one element the icon is displayed on all elements - dmitriy kiryushin
 $('.msg').hover(function(e) { $('a',this).show(); }, function(e) { $('a',this).hide(); });