views:

52

answers:

1

Not sure what I'm doing wrong with .live()

$(function(){
        var wrapper = $('#trailer_wrapper');
        var button = $('h2 a');

        button.click(function() {
            wrapper.fadeIn(2000);
            button.addClass('selected');
            button.text('close ×');
            return false;
        });

        $('h2 a.selected').live('click', function() {
            wrapper.fadeOut(2000);
            $(this).removeClass('selected');
            button.text('Watch Trailer »');
            return false;
        });
    });
+5  A: 

Well your "click" handler (the "direct" one) is still going to run, and it'll run before the "live" handler.

Try just having the click handler:

button.click(function() {
  if ($(this).is('.selected')) {
    wrapper.fadeOut(2000);
    $(this).removeClass('selected');
    button.text('Watch Trailer »');
  }
  else {
    wrapper.fadeIn(2000);
    button.addClass('selected');
    button.text('close ×');
  }
  return false;
});
Pointy
Correct!, but I'm out of upvotes for today, can't believe I glanced completely over that, good catch.
Nick Craver
Yeah, I totally wasn't thinking on this one. Thanks for clearing that up.
michaelespinosa