JQuery: How to select all elements with same class besides the one clicked?

Antonio Moore picture Antonio Moore · Apr 18, 2012 · Viewed 9.2k times · Source

I guess I'm looking for an opposite to $(this).

If I have 3 elements with the same class how can I select only the elements I haven't clicked. EG:

<div class="test"></div>
<div class="test"></div>
<div class="test"></div>

And I click the middle div, how do I only implement effects on the first and last element?

Answer

Nicola Peluchetti picture Nicola Peluchetti · Apr 18, 2012

You could use not():

$('div.test').click(function(){
    var notClicked =  $('.test').not(this);
});