149
var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];
alert(a==b)
alert(a==$(b))

It's always false. How can you compare two elements in jQuery?

thanks

DarkAjax
  • 15,955
  • 11
  • 53
  • 65
zjm1126
  • 63,397
  • 81
  • 173
  • 221

6 Answers6

366

For the record, jQuery has an is() function for this:

a.is(b)

Note that a is already a jQuery instance.

Inferpse
  • 4,135
  • 1
  • 31
  • 39
e-motiv
  • 5,795
  • 5
  • 27
  • 28
  • 1
    This method also has the advantage of working where the "==" solution doesn't: `$(event.target).is($("#a"))` – Tyson Nov 28 '12 at 03:49
  • 1
    @DanielS I don't think your 2 added "$(..)" are required here, since in the question a and b are already jquery instances. I also just mention it for other people who might think you have to double load. – e-motiv Dec 04 '12 at 16:28
  • If you have elemA that is a jQuery variable that represents an object `

    `, and you create elemB using `var divNode = document.createElement("div"); var pNode = document.createElement("p"); var elemB = $(divNode).html(pNode);`, these do NOT appear as equal/make an `if` statement equal true when you do `if (elemA.is(elemB)) { .... } `. This did not work for me.
    – vapcguy Jun 19 '15 at 21:17
  • @Tyson also `$("#a").is(event.target)` works and uses one less jQuery object. –  Aug 11 '15 at 18:36
155

You could compare DOM elements. Remember that jQuery selectors return arrays which will never be equal in the sense of reference equality.

Assuming:

<div id="a" class="a"></div>

this:

$('div.a')[0] == $('div#a')[0]

returns true.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

Every time you call the jQuery() function, a new object is created and returned. So even equality checks on the same selectors will fail.

<div id="a">test</div>

$('#a') == $('#a') // false

The resulting jQuery object contains an array of matching elements, which are basically native DOM objects like HTMLDivElement that always refer to the same object, so you should check those for equality using the array index as Darin suggested.

$('#a')[0] == $('#a')[0] // true
Anurag
  • 140,337
  • 36
  • 221
  • 257
10
a.is(b)

and to check if they are not equal use

!a.is(b)

as for

$b = $('#a')
....
$('#a')[0] == $b[0] // not always true

maybe class added to the element or removed from it after the first assignment

Jondi
  • 431
  • 4
  • 8
2

Random AirCoded example of testing "set equality" in jQuery:

$.fn.isEqual = function($otherSet) {
  if (this === $otherSet) return true;
  if (this.length != $otherSet.length) return false;
  var ret = true;
  this.each(function(idx) { 
    if (this !== $otherSet[idx]) {
       ret = false; return false;
    }
  });
  return ret;
};

var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];

console.log($(b).isEqual(a));
gnarf
  • 105,192
  • 25
  • 127
  • 161
-1

The collection results you get back from a jQuery collection do not support set-based comparison. You can use compare the individual members one by one though, there are no utilities for this that I know of in jQuery.

Frank Schwieterman
  • 24,142
  • 15
  • 92
  • 130