Read more

Compare two jQuery objects for equality

Henning Koch
December 27, 2012Software engineer at makandra GmbH

Every time you call $(...) jQuery will create a new object. Because of this, comparing two jQuery collections with == will never return true, even when they are wrapping the same native DOM elements:

$('body') == $('body') // false
Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

In order to test if two jQuery objects refer to the same native DOM elements, use is:

var $a = $('body');
var $b = $('body');
$a.is($b); // true

Jasmine equality matcher for jQuery

See here for a custom Jasmine matcher that tests if two jQuery objects are equal.

Posted by Henning Koch to makandra dev (2012-12-27 19:49)