Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - run function on each element except the one that was clicked

As some example 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 don't want the element that was clicked to animate to 140px width and then back to 160px.

Is there a way to run 'each' on only the elements in a set who were not clicked? Or is there a better way?

like image 628
Matthew Avatar asked Dec 15 '10 01:12

Matthew


1 Answers

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') , so :

$('a.parent').not(this).each(function(){
                $(this).stop(true,false).animate({
                    width: '140px'
                },200,function(){
                });
            });
like image 139
Sina Fathieh Avatar answered Sep 19 '22 16:09

Sina Fathieh