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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)