Slick23
Slick23

Reputation: 5897

Is there a way to filter matched elements that didn't fire event in jQuery?

I have a lot of elements with a .photo class on my page.

I select them this way:

 $('#photos-container .photo').hover ...

Is there a way to filter out the elements that didn't fire the hover event?

Upvotes: 2

Views: 64

Answers (4)

Dennis
Dennis

Reputation: 32598

Similar to DaDaDom's answer, but it caches the elements so you don't reselect on every event.

var photos = $('#photos-container .photo').hover(function(e) {
  var elementsThatDidntFire = photos.not(this);
});

Upvotes: 2

Esailija
Esailija

Reputation: 140220

$('#photos-container .photo').hover(function(e) {
    var elementsThatDidntFire = $('#photos-container .photo').filter(function() {
        return this !== e.target;
    });
}, function() {

});

http://jsfiddle.net/aUmNK/3/

Upvotes: 1

Samich
Samich

Reputation: 30115

Use .not filtering:

$('#photos-container .photo').hover(function() {
    $('#photos-container .photo').not(this).addClass('hover');
}, function() {
    $('#photos-container .photo').not(this).removeClass('hover');
});

Code: http://jsfiddle.net/T6xGj/5/

Upvotes: 2

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6466

What about

$('#photos-container .photo').hover(function(e) {
  var elementsThatDidntFire = $('#photos-container .photo').not(this);
}

Upvotes: 6

Related Questions