Note that the code repeats in both if and else , that is, it always executes. Just make it out of the statement:
if (index > 2) { index -= 3; } var elt = $(this).closest('.outer_wrapper').prev('.outer_wrapper'); elt.find('.tabSlide').removeClass('tabShow'); elt.find('.tabSlide:eq(' + index + ')').addClass('tabShow');
Then notice that the jQuery object is just an array. You can simplify your code this way:
if (index > 2) { index -= 3; } var elt = $(this).closest('.outer_wrapper').prev('.outer_wrapper').find('.tabSlide'); $(elt.removeClass('tabShow')[index]).addClass('tabShow');
Finally, we can exclude the aux variable, used just to demonstrate how you call the same object:
if (index > 2) { index -= 3; } $($(this).closest('.outer_wrapper').prev('.outer_wrapper').find('.tabSlide').removeClass('tabShow')[index]).addClass('tabShow');
Please split this into several lines of code: D
[EDIT]
Well, and just for fun, here is an even more extreme code like an astronaut, getting rid of the remaining if :
$($(this).closest('.outer_wrapper').prev('.outer_wrapper').find('.tabSlide').removeClass('tabShow')[(index <= 2 ? index : index - 3)]).addClass('tabShow');
BUT! This is terribly unreadable and IMO, you should only stick to the first step. While this is not a performance issue, do not overdo it. Applying the DRY rule due to readability / maintainability or just inserting everything into one line of code makes the same sense as reading and writing abbreviated code. That is, do not do this :).
[EDIT 2]
@StuartNelson reminded me of the existence of the $.eq() function, which would lead to this final code (split into several lines):
$(this).closest('.outer_wrapper') .prev('.outer_wrapper') .find('.tabSlide') .removeClass('tabShow') .eq(index <= 2 ? index : index - 3) .addClass('tabShow');