Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery compare two DOM object?

Clicking on an element:

$('.my_list').click(function(){
   var selected_object = $(this);

   $('.my_list').each(function(){
      var current_object = $(this);

      if( selected_object == current_object ) alert('FOUND IT !');
   });
});

I don't know why, but I don't get the alert message "FOUND IT !".

like image 370
Zbarcea Christian Avatar asked Mar 04 '13 08:03

Zbarcea Christian


1 Answers

You can use the jQuery.is function:

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

if (selected_object.is(current_object)) {
   ...    
}

An alternate solution is to use jQuery.get function to get the raw elements and compare them using == or === operator:

if (selected_object.get(0) == current_object.get(0)) {
   ...
}

jsFiddle demo

like image 59
Salman A Avatar answered Oct 16 '22 01:10

Salman A