2
  $('.toggle').toggle(function(){
    var ul = $('ul');
    ul.fadeOut('fast', function(){
      ul.fadeIn('fast').removeClass('off');
      ul.addClass('on');
    });
  }, function(){
    ul.fadeOut('fast', function(){
      alert('wtf'); // <- never gets here...
      ul.fadeIn('fast').removeClass('on');
      ul.addClass('off');
    });
  });

What am I doing wrong??

Any code that I add inside the second fadeOut callback function never gets executed...

Alex
  • 66,732
  • 177
  • 439
  • 641

3 Answers3

3

Put var ul = $('ul'); in your second function too.

Plynx
  • 11,341
  • 3
  • 32
  • 33
2

ul is not defined in the second callback. Change its scope so it is visible to both callbacks.

var ul = $('ul');

$('.toggle').toggle(function(){
  ul.fadeOut('fast', function(){
    ul.fadeIn('fast').removeClass('off');
    ul.addClass('on');
  });
}, function(){
  ul.fadeOut('fast', function(){
    ul.fadeIn('fast').removeClass('on');
    ul.addClass('off');
  });
});

If you don't want ul to be in scope outside of this, either wrap it in a self invoking function or specify the ul within each callback of toggle().

alex
  • 479,566
  • 201
  • 878
  • 984
2

The variable ul doesn't exist in the scope of the second function. It's actually a bit of an unneeded optimization in your case, since the following should also work:

  $('.toggle').toggle(function(){
    $('ul').fadeOut('fast', function(){
      $(this).fadeIn('fast').removeClass('off').addClass('on');
    });
  }, function(){
    $('ul').fadeOut('fast', function(){
      $(this).fadeIn('fast').removeClass('on').addClass('off');
    });
  });
Hamish
  • 22,860
  • 8
  • 53
  • 67