1

I have the following menu

<ul>
   <li class="button">One</li>
   <li class="button">Two</li>
   <li class="button">Three</li>
   <li class="button">Four</li>
   <li class="button">Five</li>
</ul>

I need to hide only the button that I click on, currently my code is hiding all buttons.

$(".button").click(function() {
    $(".button").hide( "slow");
});
sao
  • 1,835
  • 6
  • 21
  • 40

2 Answers2

4
$(".button").click(function() {
    $(this).hide("slow");
});

The this jQuery selector will select the element that sent the function call, in this case the button that was clicked.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
jamespick
  • 1,974
  • 3
  • 23
  • 45
2

Inside the event handler this will refer to the clicked element, if you select this with jQuery you can call the hide function:

$(".button").click(function() {
    $(this).hide("slow");
});
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189