robit muzaki robit muzaki - 6 months ago 9
Javascript Question

jQuery Multiple $(this) not working

I use two $(this) in a function but the second $(this) is not working

$('.port-cat li').on( 'click', function() {
$(this).addClass('current-cat').siblings().removeClass('current-cat');
$(this).parents('.portfolio_cat').removeClass('current-cat'); // this code not working });


But If I remove the first $(this) then the code will start working

$('.port-cat li').on( 'click', function() {
$(this).parents('.portfolio_cat').removeClass('current-cat'); // it's working now });


How to make the both $(this) code working without removing one?

Answer

You have add an other class by use

$(this).addClass('current-cat').siblings().removeClass('current-cat');

so the li element now has two class. You can't use $(this) any more, but you could use $("classname") to replace $(this).