Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 599
Antonio Moore Avatar asked Apr 18 '12 10:04

Antonio Moore


People also ask

How can I select an element with multiple classes in jQuery?

In jQuery, we can select elements with multiple classes using the . class selector.

How to select element by class jQuery?

The . class selector selects all elements with the specific class. The class refers to the class attribute of an HTML element. The class attribute is used to set a particular style for several HTML elements.

How do you select all elements by tag?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

What is difference between the ID selector and class selector in jQuery?

Differentiate the concepts of ID selector and class selector: The only difference between them is that “id” is unique in a page and it is applied to one HTML element while “class” selector can apply to multiple HTML elements.


2 Answers

You could use not():

$('div.test').click(function(){
    var notClicked =  $('.test').not(this);
});
like image 106
Nicola Peluchetti Avatar answered Oct 11 '22 12:10

Nicola Peluchetti


Try this:

$('.test').bind('click', function() {
  var not_clicked_elements = $(this).siblings('.test');
};
like image 26
Juan G. Hurtado Avatar answered Oct 11 '22 13:10

Juan G. Hurtado