views:

25

answers:

1
$('.tabs a ').click(function () {
    var a = $(this).attr('href');
    if (a == '#tab-1') {
        $('.btn-buy').hide();
        $('.btn-sell').show();
    } else {
        $('.btn-sell').hide();
        $('.btn-buy').show();
    }
    return false;
});

... it works, but the code is ugly, too many lines. Can it be reduced any further?

Thanks in advance for your help!

+1  A: 

You could just use toggle:

$(".tabs a").click(function() {
    $(".btn-buy").toggle();
    $(".btn-sell").toggle();
});

This would assume they start out in their correct state initially...

Dean Harding
WoW! Perfect, thanks. :)
Nimbuz
But this is not the same behaviour. If I understood correctly, it should only show the `buy` button if `#tab1` is clicked. This always toggles the state. So if I click on `tab2` twice or once on `tab2` and then `tab3`, the button shows again.
Felix Kling
There're only two tabs, so this works perfectly. But I just noticed IE doesn't show the btn-sell, hmm...
Nimbuz
@Felix Kling: yeah, you're right actually... oh well, looks like the original code is the only way to get the desired behaviour :)
Dean Harding
@Nimbuz: If it works for you it is definitely ok for me ;)
Felix Kling