Randy Gonzalez
Randy Gonzalez

Reputation: 445

jQuery click variable scope issue

I set up this fiddle with an issue I am encountering. I need the script to hide the button that was clicked after the animation is complete. What is the correct way to do this?

http://jsfiddle.net/digitalaxis/utJKU/

HTML:

<div>
    <a id='button1' href="#">Button 1</a>
    <a id='button2' href="#">Button 2</a>
    <a id='button3' href="#">Button 3</a>
    <a id='button4' href="#">Button 4</a>
    <a id='button5' href="#">Button 5</a>
</div>
<div id="box">
    Some element
</div>

JS:

$('a[id^="button"]').click(function() {    
    $('#box').hide('slow', function() {        
        $('a').hide('slow');
    });
});

Upvotes: 0

Views: 498

Answers (1)

Ricardo Binns
Ricardo Binns

Reputation: 3246

set a global variable where receive $(this), and use in the call back, like this:

$('a[id^="button"]').click(function() {
   var $this = $(this);
   $('#box').hide('slow', function() {
        $this.hide('slow');
   });
});​

Upvotes: 8

Related Questions