Jquery - run a function for every item except the one that was clicked

As some sample code, I might have something like this:

$('a.parent').click(function(){ $('a.parent').each(function(){ $(this).stop(true,false).animate({ width: '140px' },200,function(){ }); }); $(this).animate({ width: '160px' },200,function(){ }); }); 

The problem is that I do not want the element that was clicked to resize to 140 pixels and then return to 160 pixels.

Is there a way to start β€œeach” only for items in a set that have not been clicked? Or is there a better way?

+6
javascript jquery
source share
1 answer

you can use: not (this), so change:

  $('a.parent').each(function(){ $(this).stop(true,false).animate({ width: '140px' },200,function(){ }); }); 

to:

 $('a.parent:not('+this+')').each(function(){ $(this).stop(true,false).animate({ width: '140px' },200,function(){ }); }); 

or use .not (this) after $ ('a.parent'), therefore:

 $('a.parent').not(this).each(function(){ $(this).stop(true,false).animate({ width: '140px' },200,function(){ }); }); 
+22
source share

All Articles