0

I have the following code I want to edit:

$('.tab').click(function() {
    $(this).addClass('active').siblings().removeClass('active');
});

Instead of using $(this), why can't I do the following:

$('.tab').click(function() {
    $('.tab').click(function().addClass('active').siblings().removeClass('active');
});

If that is never going to work, how can I recreate the initial code without having to use $(this)?

freginold
  • 3,946
  • 3
  • 13
  • 28
Disguy
  • 39
  • 6

2 Answers2

1

Not entirely sure why you want to do this, but here is a way to avoid using "this".

$(".tab").click(function(ev){
    $(ev.currentTarget).addClass('active').siblings().removeClass('active');
});
Jordan Soltman
  • 3,795
  • 17
  • 31
0

$('.tab') refers to all elements with class="tab", $(this) refers to the one element with class="tab" that was just clicked.

For more information on this in jQuery, see jQuery: What's the difference between '$(this)' and 'this'?

Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273