Because of the same reason that $('#a) == $('#a')
is false
Each time jQuery builts a set of elements, it returns a new object (even if the jQuery object wraps the same elements as another). In JavaScript, the only time an object is equal to another, is if it's exactly the same object;
var a = {
foo: 1
};
var b = {
foo: 1
};
(a == b) // false;
To fix this, you can either compare the DOM objects directly (either by using .get(i)
or using the jQuery object like an array ([i]
)), or you get use the is()
method;
if ($('.foo').get(i) == $('.bar').get(i));
if ($('.foo')[0] == $('.bar')[0]);
if ($('.foo').is($('.bar')); // or even...
if ($('.foo').is('.bar'));
$('.foo').is('.bar')
should suffice. – Moss