Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: elements with class but not $(this)

I have a gallery with pictures and on mouseover on 1 picture I'd like to do sth with all the OTHER pictures (so all pictures having the class .picture, except the one hovering ($(this)).

I tried to apply a class to all pictures with the class .picture and then removing this added class on the picture hovering, and then using the added class (which contains the right pictures then), but it is not working smoothly...

Is there any cleaner and better way to do this?

Example:

pic 1 -> do sth with this
pic 2 -> do sth with this
pic 3 -> gets hovered, don't do anything with this, exclude it from the selection!
like image 913
Helmut Avatar asked Mar 17 '26 00:03

Helmut


2 Answers

Use .not.

$('.picture').not(this);
like image 170
gray state is coming Avatar answered Mar 18 '26 13:03

gray state is coming


var img = $('.picture');
img.on('hover', function() {
    img.not(this).each(function() {
       /* do Something with $(this); */
    })
});
like image 33
Fabrizio Calderan Avatar answered Mar 18 '26 14:03

Fabrizio Calderan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!